【问题标题】:DataGridView and DataTable数据网格视图和数据表
【发布时间】:2012-01-01 22:21:34
【问题描述】:

我的“玩家”有 DataGridView 和 DataTable。

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

当用户单击网格中的任何单元格时,我想在双击事件中访问 Player 对象。 怎么办?

【问题讨论】:

  • Grid.DataSource = PlayerList; 也会这样做。
  • @HenkHolterman:你不知道它是什么类型,也不知道他们是否要绑定到副本。
  • @HenkHolterman 不,它不会在我的程序中做同样的事情。

标签: c# .net datagridview datatable


【解决方案1】:

为DataGridView的CellContentDoubleClick事件添加一个handler,然后访问该行的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;
}

【讨论】:

  • 我是瞎还是DataRow中没有DataBoundItem属性?
  • 不,没有,但 DataGridViewRow 中有,这是 Rows 集合应该返回的内容。但是,我刚刚意识到 C# 存在翻译问题:DataGridView1.Rows(e.RowIndex) 应该是 DataGridView1.Rows[e.RowIndex]。我已经确定了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
相关资源
最近更新 更多