C# DataGridView和DataTable
我的“玩家”有DataGridView和DataTableC# DataGridView和DataTable,c#,.net,datagridview,datatable,C#,.net,Datagridview,Datatable,我的“玩家”有DataGridView和DataTable DataTable dt=Extensions.ToDataTable(PlayerList); Grid.DataSource=dt; 当用户单击我网格中的任何单元格时,我想在双击事件中访问Player objet。 如何操作?为DataGridView的CellContentDoubleClick事件添加处理程序,然后访问该行的DataBoundItem: DataGridView1.CellContentDoubleClick
DataTable dt=Extensions.ToDataTable(PlayerList);
Grid.DataSource=dt;
当用户单击我网格中的任何单元格时,我想在双击事件中访问Player objet。
如何操作?为DataGridView的CellContentDoubleClick事件添加处理程序,然后访问该行的DataBoundItem:
DataGridView1.CellContentDoubleClick += DataGridView1_CellContentDoubleClick;
private void DataGridView1_CellContentDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
Player player = DataGridView1.Rows[e.RowIndex].DataBoundItem as Player;
}
为DataGridView的CellContentDoubleClick事件添加处理程序,然后访问该行的DataBoundItem:
DataGridView1.CellContentDoubleClick += DataGridView1_CellContentDoubleClick;
private void DataGridView1_CellContentDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
Player player = DataGridView1.Rows[e.RowIndex].DataBoundItem as Player;
}
Grid.DataSource=PlayerList
也会这样做。@HenkHolterman:你不知道它是什么类型,或者他们是否要绑定到副本。@HenkHolterman不,在我的程序中不会这样做。Grid.DataSource=PlayerList
也会这样做。@HenkHolterman:你不知道它是什么类型,或者他们是否想绑定到副本。@HenkHolterman不,在我的程序中不会这样做。我是盲的还是DataRow中没有DataBoundItem属性?不,没有,但DataGridViewRow中有,这是Rows集合应该返回的。然而,我刚刚意识到C#有一个翻译问题:DataGridView1.Rows(e.RowIndex)
应该是DataGridView1.Rows[e.RowIndex]
。我已经修复了答案。我是盲的还是DataRow中没有DataBoundItem属性?不,没有,但DataGridViewRow中有,这是Rows集合应该返回的。然而,我刚刚意识到C#有一个翻译问题:DataGridView1.Rows(e.RowIndex)
应该是DataGridView1.Rows[e.RowIndex]
。我已经确定了答案。