Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView和DataTable_C#_.net_Datagridview_Datatable - Fatal编程技术网

C# DataGridView和DataTable

C# 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

我的“玩家”有DataGridView和DataTable

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]
。我已经确定了答案。