【发布时间】:2011-01-22 11:32:20
【问题描述】:
我希望在用户单击单元格时出现 OpenFileDialog,然后在单元格中显示结果。
一切正常,除了 DataGridView 显示一个额外的行,用于将值添加到它绑定到的列表中。如果dataGridView.AllowUserToAddNewRows == true,则显示该行,这就是我想要的。我不希望以编程方式编辑该行时应用程序崩溃;相反,它应该完全按照用户手动编辑该行的方式执行(将新行添加到基础列表,将另一个空行推送到网格以添加值)。
我阅读了有关 SendKeys.Send() 的信息,它应该使 DataGridView 的行为与用户输入的值完全相同;但是,它也不起作用。这是我正在尝试的:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.CurrentCell = cell;
//simply doing a cell.Value = etc. will cause the program to crash
cell.ReadOnly = false;
dataGridView1.Columns[cell.ColumnIndex].ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.BeginEdit(true);
SendKeys.Send(openFileDialog1.FileName + "{Enter}");
dataGridView1.EndEdit();
cell.ReadOnly = true;
dataGridView1.Columns[cell.ColumnIndex].ReadOnly = true;
}
//I would expect the FileName would be in the cell now, and a new empty
//row tacked onto the end of the DataGridView, but it's not; the DataGridView
//is not changed at all.
【问题讨论】:
-
设置
cell.Value时遇到什么样的异常? -
@Zach:当我点击空单元格时,它会正确填写值,但不会添加另一个空行。当我在该行之外单击时,最后一行上所有单元格中的值都会消失(这只发生在最后一行)。当我再次单击最后一行时,我得到一个 InvalidOperationException:“由于对象的当前状态,操作无效。” dataGridView1_CellClick 是我在该表单上处理的唯一事件(如上所示)
标签: c# .net data-binding datagridview