如何在Button Click事件上从DataGridView填充TextBox
我想从DataGridView控件填充按钮单击事件的数据:
我的代码是这样的
for (int i = 0; i < dgv_EmpAttList.Columns.Count; i++) { txt_EnrollNo.Text = this.dgv_EmpAttList.CurrentRow.Cells[i].Value.ToString(); txt_FirstInTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[i].Value.ToString(); txt_LastOutTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[i].Value.ToString(); txt_TotalHours.Text = this.dgv_EmpAttList.CurrentRow.Cells[i].Value.ToString(); }
在你的专栏中循环可能无济于事。
尝试为每个文本框分配它映射到的单元格:
txt_EnrollNo.Text = this.dgv_EmpAttList.CurrentRow.Cells[1].Value.ToString(); txt_FirstInTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[2].Value.ToString();
为什么你必须使用循环? 你可以写
txt_EnrollNo.Text = this.dgv_EmpAttList.CurrentRow.Cells[0].Value.ToString(); txt_FirstInTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[1].Value.ToString(); txt_LastOutTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[2].Value.ToString(); txt_TotalHours.Text = this.dgv_EmpAttList.CurrentRow.Cells[3].Value.ToString();
编辑:
然后在按钮上单击发送行以更新并编写代码,如下所示
txt_EnrollNo.Text = row.Cells[0].Value.ToString(); //where row is DataGridViewRow