【问题标题】:How to get data from the edited cell in datagridview using c# winform?如何使用 c# winform 从 datagridview 中已编辑的单元格中获取数据?
【发布时间】:2017-08-06 14:55:39
【问题描述】:

我有一个数据网格视图,它的数据源已使用列表设置。我的 datagridview 是可编辑的。如何获得编辑后的单元格新值? 我是 c# winform 的初学者。

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{         Console.WriteLine(Convert.ToString(dataGridView.Rows[e.RowIndex].Cells["id"].Value));
}

使用此代码,我得到了单元格的先前值。

【问题讨论】:

  • 我现在还没有 VS 但你可以试试这样的:dataGridView.SelectedCells[0].Value.ToString() 吗?
  • 不,它仍然给我以前的值,而不是编辑后的值。@AsfK

标签: c# winforms datagridview


【解决方案1】:

您可以使用CellValueChanged 事件。

 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
        //do your checks to see RowIndex is not -1 and other good stuffs
            var row = dataGridView1.Rows[e.RowIndex];
            var changedValue = (string) row.Cells[e.ColumnIndex].Value;
        }

【讨论】:

    【解决方案2】:

    如果您想在单元格被选中事件后立即获取值,则可以使用 CellEnter 事件,然后获取所选单元格的值,只需使用: datagridview1.CurrentCell.Value如果你想要一个字符串,你可以使用datagridview1.CurrentCell.Value.toString()

     private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        var selectedcell = dataGridView1.CurrentCell.Value;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      相关资源
      最近更新 更多