简体   繁体   中英

C# : datagridview value to textbox on form load

i have datagridview it goes like this

在此处输入图片说明

the arrow select the first row. and i want to make that first row in to textbox automatically . like the following picture

在此处输入图片说明

i use datagridview.cellClick event to make the second picture works

however, i need to make it work on constructor ( when the form started, the three texboxt already filled with 1 jodi 081234125125 ), without using datagridview.cellclick event

how do i do that ?

when i use the following code

 textBox1.Text = dataGridView1.CurrentRow.Cells["nomor_nasabah"].Value.ToString();

it shows 在此处输入图片说明

You can use this one:

private void SetFirstRowSelected()
{
   if (dataGridView1.Rows.Count > 0)
   {
      var row = dataGridView1.Rows[0];

      textBox1.Text = row.Cells[1].Value.ToString();
      //and so on

      dataGridView1.CurrentCell = row.Cells[0]; //set focus to first cell in first row
   }
}

and call it in your Form_Load event.

var row = dataGridView1.Rows[0]; means that we are selecting the very first row from grid. row variable is the reference to it just to avoid typing dataGridView1.Rows[0] every time.

dataGridView1.CurrentCell = row.Cells[0] means that we are selecting the first cell in first row (it is the cell in "nomar_nasabah" column).

textBox1.Text=dataGridView1.SelectRow[0].Cells[0].Value.ToString();

textBox2.Text=dataGridView1.SelectRow[0].Cells[1].Value.ToString();

textBox3.Text=dataGridView1.SelectRow[0].Cells[2].Value.ToString();

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