2013-08-07 27 views
1

我有datagridview它是這樣C#:datagridview的值到文本框上形式負載

enter image description here

箭頭選擇第一行。我想自動將第一行寫入textbox。像下面的圖片

enter image description here

我使用datagridview.cellClick事件作出第二張照片作品

但是,我需要它的構造函數的工作(的形式開始的時候,已經充滿了1三個texboxt jodi081234125125),不使用datagridview.cellclick事件

我該怎麼做?

當我使用下面的代碼

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

它顯示 enter image description here

+0

通過在Form_Load事件方法中包含與單元格中包含的代碼相同的代碼? – varocarbas

+0

我打算把代碼放到我的構造函數中。我的構造函數而不是Form_Load事件 – Cignitor

+0

爲什麼不在form_load事件中?你必須讓所有的控件在與它們交互之前被「正確地創建」;也許這就是你得到這個錯誤的原因。 – varocarbas

回答

1

您可以使用此一:

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 
    } 
} 

,並調用它在你的事件。

var row = dataGridView1.Rows[0];意味着我們正在從網格中選擇第一行。 row變量只是爲了避免每次輸入dataGridView1.Rows[0]

dataGridView1.CurrentCell = row.Cells[0]表示我們正在選擇第一行中的第一個單元格(它是「nomar_nasabah」列中的單元格)。

+0

這條線是什麼意思? 'var row = dgv.Rows [0];'這是一個'dgv.CurrentCell = row.Cells [0];'是指? 'focus','currentRow'和'selectedRow'之間有什麼區別? – Cignitor

+0

看看我的編輯,看看現在更清楚了嗎? – gzaxx

+0

現在它更清晰了,所以var行就像'datagridview1.rows'的簡碼一樣' – Cignitor

0

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

相關問題