2012-01-01 50 views
0

我有我的“玩家”DataGridView和DataTable。DataGridView和DataTable

 DataTable dt = Extensions.ToDataTable<Player>(PlayerList); 
     Grid.DataSource = dt; 

我想在用户单击网格中的任何单元格时访问doubleclick事件中的Player objet。 如何做到这一点?

+0

'Grid.DataSource = PlayerList;'会做同样的。 – 2012-01-01 22:23:52

+0

@HenkHolterman:你不知道它是什么类型,或者他们是否想要绑定到一个副本。 – SLaks 2012-01-01 22:31:13

+0

@HenkHolterman不,它不会在我的程序中做同样的事情。 – Hooch 2012-01-01 22:59:31

回答

3

添加的处理程序的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; 
} 
+0

我瞎了还是DataRow中没有DataBoundItem属性? – Hooch 2012-01-01 23:01:36

+0

不,没有,但DataGridViewRow中存在,这是Rows集合应返回的内容。但是,我刚刚意识到C#存在翻译问题:“DataGridView1.Rows(e.RowIndex)'应该是'DataGridView1.Rows [e.RowIndex]'。我已经解决了答案。 – 2012-01-01 23:08:05