用C#在textbox中显示datagridview中当前行的值很简单,这里不多写。
一个拓展的需求是当鼠标换行时,textbox同步的显示选中的值。以下是思路和code:
使用CurrentCellChanged方法同步显示grid中的值
code:
this.DataViewMain.CurrentCellChanged +=new System.EventHandler(DataViewMain_CurrentCellChanged);
note: 在griddataview中必须将SelectionMode修改为CellSelect。
然后再code中通过以下代码显示grid中的值:
int rownum = this.DataViewMain.CurrentCell.RowIndex;
this.TextBox1.Text = this.DataViewMain.Rows[rownum].Cells[0].Value.ToString();