直接在DataGridView中更改值

时间:2012-10-08 12:06:26

标签: c# datagridview cell windows-forms-designer

如何更改DataGridView Windows窗体中所有单元格的值?我想直接在Windows窗体中更改,比如直接在单元格中输入

我有一张表:

AA    BB    CC
--------------
1     aa    ac
2     bb    fd// I type here and change the value to kk

代码:

DataGridViewTextBoxColumn AA= new DataGridViewTextBoxColumn();
MsgIDHex.HeaderText = "AA";
MsgIDHex.DataPropertyName = "AA";
DataGridViewTextBoxColumn BB= new DataGridViewTextBoxColumn();
MsgIDHex.HeaderText = "BB";
MsgIDHex.DataPropertyName = "BB";
DataGridViewTextBoxColumn CC= new DataGridViewTextBoxColumn();
MsgIDHex.HeaderText = "CC";
MsgIDHex.DataPropertyName = "CC;
dataGridView1.DataSource = result;
dataGridView1.Columns.AddRange(AA, BB, CC};

我应该使用DataGridViewTextBoxEditingControl做什么吗?

4 个答案:

答案 0 :(得分:4)

您可以这样做(假设您想以编程方式更改一个值):

dataGridView1.Rows[RowNumber].Cells[CC.Index].Value = newValue;

已经解释了如何启用某些单元格的编辑here

答案 1 :(得分:1)

如果要将数据源设置为对象,则链接到某个字段的属性必须具有setter才能更改该值。否则它是一个只读属性,将被视为这样。不确定这是否有帮助,它有点不清楚你在追求什么,以及你正在使用什么。

答案 2 :(得分:0)

// Retrieve the cell value for the cell in the Name column at row 4.
String testValue2 = (String)dataGridView1["Name", 4].Value;

答案 3 :(得分:0)

public void UpdateDataGridView(int cindex, string value)
{
    if (InvokeRequired)
    {
        this.Invoke(new Action<int,string>(UpdateDataGridView), new object[] { cindex, value });
        return;
    }

    int selectedRowIndex = dataGridView1.SelectedRows[0].Index;
    dataGridView1.Rows[selectedRowIndex].Cells[cindex].Value = value;
}