用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();