【发布时间】:2012-01-23 00:07:32
【问题描述】:
我想在我的DataGridView 中访问我的Player 对象。
这就是我将我的玩家列表绑定到DataGridView的方式:
PlayerList = new List<Player>(players);
//Populate Player list heere (...)
DataTable dt = Tools.ToDataTable<Player>(PlayerList);
Grid.DataSource = dt;
现在我想在双击事件的选定行中访问我的Player 对象:
private void Grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (Grid.SelectedCells == null)
{
return;
}
int row = Grid.SelectedCells[0].RowIndex;
//Here I get that exception from the title
((Player)(Grid.Rows[row].DataBoundItem)).KillPlayer();
}
【问题讨论】:
-
不确定,但我假设通过将
List转换为DataTable,您会丢失实际的Player实例 - 尝试使用PlayerList[row].KillPlayer();直接访问该实例并查看它是否有效。 -
这在很大程度上取决于 Tools.ToDataTable 的作用。
标签: c# .net winforms collections datagridview