今日使用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));