我正在尝试将结构表绑定到DataGridView。加载和查看表工作正常,但我无法编辑值并将其存储回表中。这就是我要做的。
我有一个“原始”数据类型,real由
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,初始化它并将其显示在网格中。该网格允许编辑,并设置了用于手机显示和设置手机格式的事件,如下所示:
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);
}
当我编辑单元格中的值时,我可以更改文本。离开手机后,手机会触发并调用事件处理程序。进入手机处理程序似乎一切正常。E.所需类型为Myreal。e.value是一个带有新值的字符串。从字符串创建新myreal之后,e.value设置正确。rowindex和columnindex正确。只读设置为假。
但是,当我离开单元格时,系统会将原始值还原到单元格。我以为updateCellValue会替换数据源中的值,但我似乎遗漏了一些东西。
我错过了什么?
谢谢,
马克斯