【问题标题】:Value of Selected dataGridView cell in Textbox文本框中选定的 dataGridView 单元格的值
【发布时间】:2012-11-14 07:24:14
【问题描述】:
我有一个windows窗体中的datagridview和文本框,当我点击datagridview的一个单元格时,值必须复制到文本框。
我收到一个错误:
System.Windows.Forms.DataGridCell 不包含 RowIndex 的定义
我试过这个代码
void dataGridView1_Click(object sender, EventArgs e)
{
Txt_GangApproved.Text=dataGridView1.CurrentCell.RowIndex.Cells["NO_OF_GANGS_RQRD"].Value.ToString();
}
【问题讨论】:
标签:
c#
datagridview
textbox
【解决方案1】:
foreach (DataGridViewRow RW in dataGridView1.SelectedRows) {
//Send the first cell value into textbox'
Txt_GangApproved.Text = RW.Cells(0).Value.ToString;
}
【解决方案2】:
试试这个-
Txt_GangApproved.Text = dataGridView1.SelectedRows[0].Cells["NO_OF_GANGS_RQRD"].Value.ToString();
【解决方案3】:
您使用错误的事件来实现您想要的。不要使用 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();
}
}
【解决方案4】:
这是 100% 工作代码(使用 -CellClick- 事件处理程序):
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.CurrentCell.Value.ToString();
}
【解决方案5】:
当我的 DataGridView 的选择模式为 FullRowSelect 时,我有时会使用 SelectionChanged 事件。然后我们可以在事件中写一行:
Txt_GangApproved.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["NO_OF_GANGS_RQRD"].Value);
【解决方案6】:
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();
}
}