我试图将结构的表绑定到DataGridView。加载和查看表格工作正常,但我无法编辑值并将其存回表格中。这是我正在做的。绑定DataGridView不更新数据源
我有一个“原始”数据类型,通过
public struct MyReal:IMyPrimative
{
public Double m_Real;
//...
public MyReal(String val)
{
m_Real = default(Double);
Init(val);
}
//...
}
真正定义它得到在结构中使用:
public struct MyReal_Record : IMyRecord
{
public MyReal Freq { get; set;}
MyReal_Record(String[] vals)
{
Init(vals);
}
}
并且该结构被用于利用一个通用的绑定定义一个表列表
public class MyTable<S> : BindingList<S> where S: struct, IMyRecord
{
public Type typeofS;
public MyTable()
{
typeofS = typeof(S);
// ...
}
该表格被动态地用作网格的绑定源。
private void miLoadFile_Click(object sender, EventArgs e)
{
MyModel.Table<Real_Record> RTable = new MyModel.Table<Real_Record>();
//... Table initialized here
//set up grid with virtual mode
dataGridView1.DataSource = RTable;
}
所有这些工作正常,我可以创建RTable,初始化它并将其显示在网格中。该网格允许编辑并具有CellParsing设置事件和CellFormatting看起来像:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.DesiredType != typeof(String))
return;
e.Value = e.Value.ToString();
}
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
if (e.DesiredType != typeof(MyReal))
return;
e.Value = new MyReal(e.Value.ToString());
e.ParsingApplied = true;
this.dataGridView1.UpdateCellValue(e.ColumnIndex, e.RowIndex);
}
当我在一个单元格编辑值,我可以改变文本。在离开单元格时,CellParsing触发并调用事件处理程序。一切似乎都正确进入CellParsing处理程序。 e.DesiredType是MyReal。 e.Value是具有新值的字符串。从字符串中创建新的MyReal后,e.Value设置正确。 RowIndex和ColumnIndex是正确的。 ReadOnly设置为false。
但是,当我离开单元格时,系统会将原始值恢复到单元格。我认为UpdateCellValue会替换dataSource中的值,但我似乎错过了一些东西。
我错过了什么?
感谢, 最大