2017-08-06 167 views
1

我有一个datagridview谁的数据源已被设置使用列表。我的datagridview是可编辑的。我如何获得编辑的单元格新值? 我是C#winform的初学者。如何使用c#winform从datagridview中编辑的单元格中获取数据?

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

使用此代码我得到单元格的以前的值。

+0

我没有VS,但现在你可以尝试这样的事:'dataGridView.SelectedCells [0] .Value.ToString()'? – AsfK

+0

不,它仍然给我以前的值,而不是编辑的值。@ AsfK –

回答

1

您可以使用CellValueChanged事件。 datagridview1.CurrentCell.Value如果你想有一个字符串,您可以:

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; 
     } 
+0

非常感谢..这解决了我的目的! –

0

你可以,如果你想尽快小区选择事件,然后获取所选单元格的值只需要使用获得的价值使用CellEnter事件使用datagridview1.CurrentCell.Value.toString()

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