如何以编程方式在DataGridView中设置单元格值?

39

我有一个DataGridView。其中一些单元格的数据来自串口:我想将数据推入单元格,并使其更新底层绑定的对象。

我正在尝试类似这样的操作:

SetValueFromSerial (decimal newValue)
{
    dataGridView.CurrentCell.Value = newValue;
}

使用字符串并没有帮助:

    dataGridView.CurrentCell.Value = newValue.ToString ();
在这两种情况下,我都没有看到网格中的任何内容,底层值也没有改变。
我在Google和这里搜索了一下,但是我没有找到任何东西。(也许我错过了什么,可能是一些显而易见的东西,但我并不非常懒。)

2
在修改 cell.Value 后调用 grid1.NotifyCurrentCellDirty(true); - S.Serpooshan
2
dataGridView.CurrentCell.Value = newValue.ToString (); dataGridView.editingControl.Text = newValue.ToString (); 如果您这样做,您可以立即看到更改。 - bh_earth0
15个回答

45

如果 DataGridView 已经被数据绑定,那么您不应该直接修改单元格的内容。相反,您应该修改数据绑定的对象。可以通过 DataGridViewRowDataBoundItem 访问该对象:

MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem;
obj.MyProperty = newValue;

请注意,绑定的对象应该实现INotifyPropertyChanged接口,以便更改可以反映在DataGridView中。


我猜这个工作的唯一方法是使用List<T>作为数据源?这可能不适用于DataTable作为数据源?所以我们需要一些ORM吗? - FrenkyB
1
@user867703,不一定是List<T>,任何集合都可以。如果源是DataTable,则DataBoundItem的类型将为DataRowView。 - Thomas Levesque
一种简单的方法也是在修改cell.Value后调用grid1.NotifyCurrentCellDirty(true); - S.Serpooshan
我曾经为这个问题感到沮丧,只有这种方法对我有效。不要忘记在你用于绑定的模型类中实现INotifyPropertyChanged接口。 - Peck_conyon

38
dataGridView1[1,1].Value="tes";

6
如果您因某些原因不想修改绑定数据对象(例如,您想在网格中显示某些视图,但不想将其作为数据源对象的一部分),则可能希望执行以下操作:
1.手动添加列:
    DataGridViewColumn c = new DataGridViewColumn();
    DataGridViewCell cell = new DataGridViewTextBoxCell();

    c.CellTemplate = cell;
    c.HeaderText = "added";
    c.Name = "added";
    c.Visible = true;
dgv.Columns.Insert(0, c); 

2. 在 DataBindingComplete 事件中,做如下操作:

foreach (DataGridViewRow row in dgv.Rows)
{if (row.Cells[7].Value.ToString()=="1")
row.Cells[0].Value = "number one"; }

(只是一个愚蠢的例子)

但要记住,它必须在DataBindingComplete中才能进行,否则值将保持空白。


5

我搜索了如何插入新的行并且如何设置其中单元格的值,就像Excel一样。以下是我的解决代码:

dataGridView1.ReadOnly = false; //Before modifying, it is required.
dataGridView1.Rows.Add(); //Inserting first row if yet there is no row, first row number is '0'
dataGridView1.Rows[0].Cells[0].Value = "Razib, this is 0,0!"; //Setting the leftmost and topmost cell's value (Not the column header row!)
dataGridView1[1, 0].Value = "This is 0,1!"; //Setting the Second cell of the first row!

注意:
  1. 之前我已经在设计模式下设计了列。
  2. 我从 datagridview 的属性中将行标题的可见性设置为 false。
  3. 最后一行很重要,需要理解: 当你直接给出 datagridview 的索引时,第一个数字是单元格号,第二个数字是行号!请记住这一点!
希望这可以帮到你。

4

我尝试了很多方法,唯一有效的是UpdateCellValue:

dataGridView.Rows[rowIndex].Cells[columnIndex].Value = "New Value";
dataGridView.UpdateCellValue(columnIndex, rowIndex);

我希望能够帮到您。 =)

4

Do you remember to refresh the dataGridView?

datagridview.refresh();

4

我曾经遇到过使用sql-dataadapter更新数据等方面的问题

以下方法对我来说很有效

mydatgridview.Rows[x].Cells[x].Value="test"
mydatagridview.enabled = false 
mydatagridview.enabled = true 

2
尝试这种方法:
dataGridView.CurrentCell.Value = newValue;

dataGridView.EndEdit();

dataGridView.CurrentCell.Value = newValue;

dataGridView.EndEdit();

Need to write two times...


3
为什么要将陈述句写两次?请解释原因。 - Abbas

1
在VB中,您可以使用这个。
Dim selectedRow As DataRowView
selectedRow = dg.Rows(dg.CurrentCell.RowIndex).DataBoundItem
selectedRow("MyProp") = "myValue"
dg.NotifyCurrentCellDirty(True)

感谢Saeed Serpooshan的最后一排。

1
我遇到了同样的问题,并且已经为VB.NET解决了这个问题。它是.NET Framework,因此您应该可以进行适应。我想比较一下我的解决方案,现在我看到似乎没有人像我这样解决它。
声明一个字段。 Private _currentDataView as DataView 所以循环遍历所有行并搜索包含我要更改的单元格旁边的值的单元格对我有用。
Public Sub SetCellValue(ByVal value As String)
    Dim dataView As DataView = _currentDataView

    For i As Integer = 0 To dataView.Count - 1
        If dataView(i).Row.Item("projID").ToString.Equals("139") Then
            dataView(i).Row.Item("Comment") = value
            Exit For ' Exit early to save performance
        End If
    Next
End Sub

为了让你更好地理解。 我知道ColumnName“projID”的值为139。我循环直到找到它,然后就可以更改“ColumnNameofCell”的值,在我的情况下是“Comment”。我在运行时使用它来添加注释。

dataview


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接