2012-11-14 27 views
1

我有一个datagridview和文本框在Windows窗体中,当我点击datagridview的单元格时,该值必须复制到文本框。文本框中选定dataGridView单元格的值

我得到一个错误:

System.Windows.Forms.DataGridCell Does not contain a definition for RowIndex

我已经试过这个代码

void dataGridView1_Click(object sender, EventArgs e) 
{ 
     Txt_GangApproved.Text=dataGridView1.CurrentCell.RowIndex.Cells["NO_OF_GANGS_RQRD"].Value.ToString(); 
} 

回答

1
foreach (DataGridViewRow RW in dataGridView1.SelectedRows) { 
    //Send the first cell value into textbox' 
    Txt_GangApproved.Text = RW.Cells(0).Value.ToString; 
} 
+0

你能帮助我,先生,请你能 http://stackoverflow.com/questions/14180601/passing-the-data-从文本框到选定单元格的datagridview#comment19653463_14180601 –

2

尝试这个 -

Txt_GangApproved.Text = dataGridView1.SelectedRows[0].Cells["NO_OF_GANGS_RQRD"].Value.ToString(); 
+0

你能帮我吗先生 http://stackoverflow.com/questions/14180601/passing-the-data-from-textbox-to-the-所选的小区-的-datagridview的#comment19653463_14180601 –

1

您正在使用错误的事件要达到什么你要。而不是使用点击的事件中使用dataGridView1的CellClick事件,并尝试下面的代码:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if(e.RowIndex >= 0 && e.ColumnIndex >= 0) //to disable the row and column headers 
    { 
     Txt_GangApproved.Text = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
    } 
} 
0

我使用的SelectionChanged事件有时,当我的DataGridView有其选择模式FullRowSelect。然后,我们可以写类似事件中的一行:

Txt_GangApproved.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["NO_OF_GANGS_RQRD"].Value); 
0
private void dataGRidView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.RowIndex >= 0) 
     { 
      DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex]; 
      string text = row.Cells[dataGridView1.CurrentCell.ColumnIndex].Value.ToString(); 
     } 
    } 
相关问题