简体   繁体   中英

How to Fill TextBoxes from DataGridView on Button Click Event

I want to fill data on button click event from DataGridView Control:

在此处输入图片说明

My code like this

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

Looping through your columns probably won't help here.

Try assigning each textbox the cell that it is mapped to:

txt_EnrollNo.Text = this.dgv_EmpAttList.CurrentRow.Cells[1].Value.ToString();
txt_FirstInTime.Text = this.dgv_EmpAttList.CurrentRow.Cells[2].Value.ToString();

Why you have to use looping? Simply you can write

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

EDIT:

Then on Button click send the row to update and write the code as follows

txt_EnrollNo.Text = row.Cells[0].Value.ToString(); //where row is DataGridViewRow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM