c# – 如何更改选择中DataGridView中的特定单元格
作者:互联网
使用列表框,我有以下代码来提取所选项:
private void inventoryList_SelectedIndexChanged(object sender, EventArgs e)
{
String s = inventoryList.SelectedItem.ToString();
s = s.Substring(0, s.IndexOf(':'));
bookDetailTable.Rows.Clear();
...
more code
...
}
我想为DataGridView做类似的事情,也就是说,当选择更改时,检索所选行中第一个单元格的内容.问题是,我不知道如何访问该数据元素.
任何帮助是极大的赞赏.
解决方法:
我认为这就是你要找的东西.但如果没有,希望它会给你一个开始.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//User selected WHOLE ROW (by clicking in the margin)
if (dgv.SelectedRows.Count> 0)
MessageBox.Show(dgv.SelectedRows[0].Cells[0].Value.ToString());
//User selected a cell (show the first cell in the row)
if (dgv.SelectedCells.Count > 0)
MessageBox.Show(dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString());
//User selected a cell, show that cell
if (dgv.SelectedCells.Count > 0)
MessageBox.Show(dgv.SelectedCells[0].Value.ToString());
}
标签:c,winforms,datagridview,selectionchanged 来源: https://codeday.me/bug/20190714/1457716.html