我编写了一个代码来获取DGV当前单元格的值(标记为一个-DGV内部的单元格已由用户单击鼠标选定)。现在,我想知道如何执行SET代码(将TextBox的值插入当前单元格)。 此代码(GET)可以正常工作:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBxGET.Text = dataGridView1.CurrentCell.Value.ToString();
}
问题出在这里,此代码(SET)显示行号(不是我想要的):
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = textBoxSET.Text;
}
答案 0 :(得分:0)
尝试:
dataGridView1.Rows[dataGridView1.CurrentRow.Index]
.Cells[dataGridView1.CurrentCell.ColumnIndex].Value = textBoxSET.Text;
答案 1 :(得分:0)
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txtselectedcellvalue.Text = dataGridView1.CurrentCell.Value.ToString();
//txtselectedcellvalue.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value.ToString();
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.SelectedCells.Count > 0)
txtselectedcellvalue.Text = dataGridView1.CurrentCell.Value.ToString();
//txtselectedcellvalue.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value;
}
private void btnchange_Click(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = txtvaluechange.Text;
//dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value = txtvaluechange.Text;
}
此代码对我来说很好。另外,@ Kaj的代码也很完美。请以正确的方式说明您的错误。