我想从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();
}
答案 0 :(得分:1)
在这里循环可能无济于事。
尝试为每个文本框分配映射到的单元格:
txt_EnrollNo.Text = this.dgv_EmpAttList.CurrentRow.Cells[1].Value.ToString();
txt_FirstInTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[2].Value.ToString();
答案 1 :(得分:0)
为什么你必须使用循环?只需你可以写
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