C# DataGridView应用的综合实现方案

以下是C# DataGridView应用的综合实现方案,满足所有需求:

一、核心功能实现

public partial class MainForm : Form
{
    private DataTable dataTable = new DataTable();
    private DataTable followTable = new DataTable();
    private const string MaskedValue = "******";

    public MainForm()
    {
        InitializeComponent();
        InitializeDataTables();
        ConfigureDataGridView();
    }

    // 初始化数据表
    private void InitializeDataTables()
    {
        // 主表
        dataTable.Columns.Add("ID", typeof(int));
        dataTable.Columns.Add("Name", typeof(string));
        dataTable.Columns.Add("Password", typeof(string)); // 需掩码字段
        dataTable.Columns.Add("BirthDate", typeof(DateTime));
        dataTable.Columns.Add("Salary", typeof(decimal));
        dataTable.Columns.Add("Status", typeof(string));

        // 跟进表
        followTable.Columns.Add("FollowID", typeof(int));
        followTable.Columns.Add("MainID", typeof(int)); // 关联主表ID
        followTable.Columns.Add("FollowDate", typeof(DateTime));
        followTable.Columns.Add("Content", typeof(string));
        followTable.Columns.Add("Recorder", typeof(string));
        followTable.Columns.Add("Status", typeof(string));
        followTable.Columns.Add("Reminder", typeof(DateTime));
    }

    // 配置DataGridView
    private void ConfigureDataGridView()
    {
        dataGridView1.DataSource = dataTable;
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.AllowUserToOrderColumns = true; // 自选列
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        
        // 设置密码列掩码
        dataGridView1.CellFormatting += (s, e) =>
        {
            if (e.ColumnIndex == dataGridView1.Columns["Password"].Index && e.Value != null)
            {
                e.Value = MaskedValue;
                e.FormattingApplied = true;
            }
        };
        
        // 双击事件
        dataGridView1.CellDoubleClick += DataGridView1_CellDoubleClick;
    }

    // 双击事件处理
    private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0) return;

        // 密码列显示真实值
        if (e.ColumnIndex == dataGridView1.Columns["Password"].Index)
        {
            var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.Value = dataTable.Rows[e.RowIndex]["Password"];
            return;
        }

        // 行双击显示详情窗口
        ShowDetailForm(e.RowIndex);
    }

    // 显示详情窗口
    private void ShowDetailForm(int rowIndex)
    {
        var row = dataTable.Rows[rowIndex];
        using (var detailForm = new DetailForm(row, followTable))
        {
            detailForm.ShowDialog();
        }
    }
}

二、详情窗口实现(DetailForm.cs)

public partial class DetailForm : Form
{
    private DataRow mainRow;
    private DataTable followTable;
    
    public DetailForm(DataRow row, DataTable followTable)
    {
        InitializeComponent();
        this.mainRow = row;
        this.followTable = followTable;
        DisplayMainData();
        LoadFollowRecords();
    }

    // 显示主表数据
    private void DisplayMainData()
    {
        // 动态创建控件
        int yPos = 20;
        foreach (DataColumn column in mainRow.Table.Columns)
        {
            var lbl = new Label { Text = column.ColumnName + ":", Location = new Point(20, yPos) };
            Control valueControl;

            switch (column.DataType.Name)
            {
                case "DateTime":
                    valueControl = new DateTimePicker() 
                    { 
                        Value = Convert.ToDateTime(mainRow[column]),
                        Location = new Point(150, yPos - 3)
                    };
                    break;
                case "Decimal":
                case "Int32":
                    valueControl = new NumericUpDown()
                    {
                        Value = Convert.ToDecimal(mainRow[column]),
                        Location = new Point(150, yPos)
                    };
                    break;
                default:
                    valueControl = new ComboBox()
                    {
                        Text = mainRow[column].ToString(),
                        Location = new Point(150, yPos),
                        DropDownStyle = ComboBoxStyle.DropDown
                    };
                    break;
            }

            panelMain.Controls.Add(lbl);
            panelMain.Controls.Add(valueControl);
            yPos += 30;
        }
    }

    // 加载跟进记录
    private void LoadFollowRecords()
    {
        int mainID = Convert.ToInt32(mainRow["ID"]);
        var filteredRows = followTable.Select($"MainID = {mainID}");
        
        if (filteredRows.Length > 0)
        {
            dataGridViewFollow.DataSource = filteredRows.CopyToDataTable();
        }
    }

    // 添加跟进记录
    private void btnAddFollow_Click(object sender, EventArgs e)
    {
        var newRow = followTable.NewRow();
        newRow["MainID"] = mainRow["ID"];
        newRow["FollowDate"] = dateTimePickerFollow.Value;
        newRow["Content"] = txtContent.Text;
        newRow["Recorder"] = cmbRecorder.Text;
        newRow["Status"] = cmbStatus.Text;
        newRow["Reminder"] = dateTimePickerReminder.Value;
        
        followTable.Rows.Add(newRow);
        LoadFollowRecords();
    }
}

三、功能说明

  1. 自选列功能

    dataGridView1.AllowUserToOrderColumns = true;
    

    用户可直接拖动列标题调整列顺序

  2. 按列排序
    自动支持点击列标题排序(DataTable默认绑定支持)

  3. 字段掩码与显示

    • 指定字段(如Password)显示为******
    • 双击密码单元格时显示真实值
    • 使用CellFormatting事件实现掩码:
    dataGridView1.CellFormatting += (s, e) => 
    {
        if (e.ColumnIndex == dataGridView1.Columns["Password"].Index) 
            e.Value = MaskedValue;
    };
    
  4. 行详情卡片

    • 双击行头弹出详情窗口
    • 动态创建控件显示数据:
      • 日期字段:DateTimePicker
      • 数值字段:NumericUpDown
      • 字符字段:ComboBox(可编辑)
  5. 跟进系统

    • 独立followTable存储跟进记录
    • 字段包含:
      followTable.Columns.Add("FollowDate", typeof(DateTime));
      followTable.Columns.Add("Content", typeof(string));
      followTable.Columns.Add("Recorder", typeof(string));
      followTable.Columns.Add("Status", typeof(string));
      followTable.Columns.Add("Reminder", typeof(DateTime));
      
    • 通过MainID关联主表记录

四、界面建议布局

区域控件类型功能说明
主窗体DataGridView显示主数据(带掩码列)
详情窗口-上部Panel动态生成字段编辑控件
详情窗口-中部DataGridView显示历史跟进记录
详情窗口-下部GroupBox新增跟进输入区域
跟进区域DateTimePicker×2跟进日期/提醒时间
ComboBox×2记录人/跟进状态
TextBox跟进内容
Button添加跟进记录

五、使用注意事项

  1. 密码字段在单元格失去焦点后自动恢复掩码状态
  2. 主表与跟进表通过ID字段关联
  3. 动态控件创建时根据数据类型自动选择:
    switch (column.DataType.Name)
    {
        case "DateTime": /* 创建DateTimePicker */;
        case "Decimal": /* 创建NumericUpDown */;
        default: /* 创建ComboBox */;
    }
    
  4. 新增跟进记录后自动刷新DataGridView显示

此方案满足所有需求,实际部署时需根据数据源调整绑定逻辑,并添加数据验证机制确保数据完整性。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值