【问题标题】:Change value in DataGridView directly直接更改 DataGridView 中的值
【发布时间】:2012-09-28 16:26:18
【问题描述】:

如何更改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 做点什么吗?

【问题讨论】:

  • 不清楚你想做什么,或者什么不起作用。您希望能够在网格上的单元格中输入内容吗?这应该只是工作。当你尝试时会发生什么?
  • 对不起,我没有解释的太清楚。感谢 Vyktor,我知道要编辑单元格,我应该编写 dataGridView1.BeginEdit(true);谢谢大家

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


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

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

【讨论】:

  • 是的,你是对的。它刚刚读取了属性。如何更改可以在那里读写的数据源?
  • 如果您使用自定义对象作为数据源,则相关属性需要有一个 setter 方法 public PropertyName { get;放;没有setter,数据绑定无法更新绑定对象中的值
【解决方案3】:
// Retrieve the cell value for the cell in the Name column at row 4.
String testValue2 = (String)dataGridView1["Name", 4].Value;

【讨论】:

    【解决方案4】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多