DataGirdView 新建 Row 鼠标点击 数据变成 空白 的问题

今日使用DataGridView

在Form中Init代码如下:

   dataGridView1.SetColumn(typeof(DebtNormal),ePropertyFieldFilter.All);
          
            var debtinfo = new DebtNormal();
            debtinfo.债券代码="测试";
             var row = new DataGridViewRow();
                        row.Tag = debtinfo;
                        dataGridView1.Rows.Add(row);                                                   
                    row.SetRowValues(debtinfo.ToItems(ePropertyFieldFilter.Field));
                    _dtDebtRow.Add("测试", row);

此时dataGridView1启动后一切正常,我们看到 这里使用了new  DataGridViewRow()语句


但是在运行中添加新的row,

语句如下:

  void _parseDebtNormalInfo_OnDataUpdated(DebtNormal arg_debtinfo)
        {
            this.Invoke(new Action(() =>
                {
                    if (!_dtDebtNormal.ContainsKey(arg_debtinfo.债券代码))
                        _dtDebtNormal.Add(arg_debtinfo.债券代码, arg_debtinfo);

                    //dataGridView1.DataSource = null;
                    //dataGridView1.DataSource = _dtDebtNormal.Values.ToArray();

                    var debtinfo = arg_debtinfo;

                    var row = (DataGridViewRow)null;
                    if (_dtDebtRow.TryGetValue(debtinfo.债券代码, out row))
                    {
                        Debug.Set(debtinfo.债券代码, "hello");
                    }
                    else
                    {

                        row = new DataGridViewRow();// dataGridView1.Rows[dataGridView1.Rows.Add() - 1];
                        dataGridView1.Rows.Add(row);
                        //row.Tag = debtinfo;                       
                        _dtDebtRow.Add(debtinfo.债券代码, row);
                        row.SetRowValues(debtinfo.ToItems(ePropertyFieldFilter.Field));
                    }
                   

                    var rowTest = _dtDebtRow["测试"];
                    var dinfoTest = rowTest.Tag as DebtNormal;
                    dinfoTest.主体评级 = "pig";
                    rowTest.SetRowValues(arg_debtinfo.ToItems(ePropertyFieldFilter.Field));
                    //row.SetRowValues(dinfoTest.ToItems(ePropertyFieldFilter.Field));
                }));
           
          
        }

这里我们也是用 new DataGridViewRow() 的语句, 此时,运行中数据显示正常,但是一旦鼠标点击新建出来的数据,我们会发现,变成了空白。


必须把 new DataGridViewRow() 代码改成 datagridView1.Rows.Add(); 让其内部自动添加,然后设置这个row

                        row = dataGridView1.Rows[dataGridView1.Rows.Add() - 1];
                        //row.Tag = debtinfo;                       
                        _dtDebtRow.Add(debtinfo.债券代码, row);
                        row.SetRowValues(debtinfo.ToItems(ePropertyFieldFilter.Field));


这样就没有问题了


posted on   norsd  阅读(198)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能

导航

< 2013年4月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示