如何以编程方式设置单元格值的DataGridView? [英] How to programmatically set cell value in DataGridView?

查看:162
本文介绍了如何以编程方式设置单元格值的DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView。一些细胞从一个串行口接收数据:我想推数据到细胞,并将其更新底层绑定的对象

I have a DataGridView. Some of the cells receive their data from a serial port: I want to shove the data into the cell, and have it update the underlying bound object.

我想是这样的:

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

使用字符串没有帮助:

using a string doesn't help:

    dataGridView.CurrentCell.Value = newValue.ToString ();

在这两种情况下,我没有看到任何在网格中,而潜在价值是不变的。

In both cases, I don't see anything in the grid, and the underlying value is unchanged.

我做了谷歌搜索,并在这里,但我没有发现任何东西。 (我可能错过了一些东西,或许是显而易见的,但我不的完全的懒惰。)

I did Google and search here, but I didn't find anything. (I may have missed something, perhaps something obvious, but I'm not utterly lazy.)

推荐答案

如果在 DataGridView的是数据绑定,你不应该直接修改单元格的内容。相反,你应该修改数据绑定对象。您可以通过访问该对象的 DataBoundItem 的DataGridViewRow

If the DataGridView is databound, you shouldn't directly modify the content of the cell. Instead, you should modify the databound object. You can access that object through the DataBoundItem of the DataGridViewRow :

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

请注意所绑定的对象应该实施 INotifyPropertyChanged的,使这一变化反映在 DataGridView的

Note that the bound object should implement INotifyPropertyChanged so that the change is reflected in the DataGridView

这篇关于如何以编程方式设置单元格值的DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆