[英]Value of Selected dataGridView cell in Textbox
i have a datagridview and textbox in windows form,when i click on a cell of the datagridview the value must copy to the textbox. 我在Windows窗体中有一个datagridview和文本框,当我单击datagridview的单元格时,该值必须复制到文本框。
I am getting a error: 我收到一个错误:
System.Windows.Forms.DataGridCell Does not contain a definition for RowIndex
System.Windows.Forms.DataGridCell不包含RowIndex的定义
I have tried this code 我已经尝试过此代码
void dataGridView1_Click(object sender, EventArgs e)
{
Txt_GangApproved.Text=dataGridView1.CurrentCell.RowIndex.Cells["NO_OF_GANGS_RQRD"].Value.ToString();
}
foreach (DataGridViewRow RW in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_GangApproved.Text = RW.Cells(0).Value.ToString;
}
尝试这个-
Txt_GangApproved.Text = dataGridView1.SelectedRows[0].Cells["NO_OF_GANGS_RQRD"].Value.ToString();
You are using wrong event to achieve what you want. 您正在使用错误的事件来实现所需的结果。 Instead of using Click event use CellClick event of dataGridView1 and try the below code:
代替使用Click事件,而使用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();
}
}
I use the SelectionChanged event sometimes when my DataGridView has its Selection mode to FullRowSelect. 有时在DataGridView的选择模式为FullRowSelect时,有时会使用SelectionChanged事件。 Then we can write a line inside the event like:
然后,我们可以在事件内写一行:
Txt_GangApproved.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["NO_OF_GANGS_RQRD"].Value);
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();
}
}
This is 100% working code (using -CellClick- event handler): 这是100%的工作代码(使用-CellClick-事件处理程序):
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.CurrentCell.Value.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.