【发布时间】:2014-07-14 22:22:39
【问题描述】:
我有一个由 MySQL 表填充的数据网格。这里的问题是我添加了一个 SelectionChanged 事件。
private void RegistrationDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// if the datagrid has an index value of 0 and above
if (RegistrationDataGrid.SelectedIndex >= 0)
{
// Exception gets thrown here
DataRowView row = (DataRowView)RegistrationDataGrid.SelectedItems[0];
// if the column Tag of the selected row in the datagrid is not empty
if (row["Tag"] != null)
{
//put the content of the cell into a textbox called tag
tag.Text = Convert.ToString(row["Tag"]);
}
}
}
上面的代码旨在捕获所选行的特定列中单元格的值。这很好用。
如果您选择最后一行,即始终存在的那一行,就会出现问题。底部是空的,里面什么都没有。
抛出一个无效的转换异常:“无法转换类型的对象 'MS.Internal.NamedObject' 输入 'System.Data.DataRowView"
我的假设是这与所选行为空的事实有关。 我只需要找到一种方法让事件在被选中时忽略该行。有什么线索吗?
【问题讨论】: