Skip to main content

BindingSource

在编写一个小工具的时候需要将一个自定义的集合绑定到DataGridView。DataGridView是.net 2.0中新的一个网格控件。DataGridView的功能非常的强大,我在使用中确遇到一个小小的问题。
我自定的集合如下:
public class CustomClassCollection : System.Collections.Generic.List<CustomClass>
{
}

CustomClass本身是有两个String类型的属性(Propertie)。我在WinForm的构造函数里面:

CustomClassCollection ccl = new CustomClassCollection();
dataGridView.DataSource = ccl;

虽然还没有数据,但是在DataGridView里面已经显出2个Title,即CustomClass的那2个属性,然后却遇到的个问题:我通过一个button Click事件向ccl里添加了东西,却没有找到DataGrid里面的DataBind()事件,而且dataGridView并没有自动的更新。DataGridView的Refresh()似乎和更新数据也没有关系。
在网上查找资料,找到BindingSource这个类。只需要,先将cll Bind到BindingSource上,再将BindingSource作为数据源Bind到DataGridView。向cll添加数据以后调用BindingSource的ResetBindings()就可以更新DataGridView:
先声明一个BindingSource:

BindingSource bs = new BindingSource();

再调用:

bs.DataSource = ccl;
dataGridView.DataSource = bs;

在添加数据的方法的最后调用:

bs.ResetBindings(false);


Key Word: DataGridView BindingSource 数据同步 更新

Comments

Anonymous said…
thankx a lot.
confusing me for 2 days for this problem, but solove by your article.
greets.

Popular posts from this blog

欠税了!!!!

上个礼拜,收到政府发来的信,大概知道是和缴税有关的,由于本人英文不好,而且我们缴税的方面一向是由公司负责的,以为是普通的通知。因为同事收到的信都和我的不一样,还是决定带给老大看看是什么东西。结果把老大吓了一跳,红的,居然是红的(我怎么看都只有一点淡淡的红色。)。这意味着我欠税了。在这个资本主义国家,最恐怖的事情莫过去欠税了!!!我的工作准证很可能被吊销。 下午立马跑回公司找人事部的。我居然公司成立几十年来第一个红单。。。人事部的人说还好公司一贯信誉良好,他们会和那边联系,让我先放心。 我倒是很放心,一点都不急。其实我想回家了,和女朋友的关系这几天又到了谷底,想回去了,虽然房子钱还没有着落。

ipod touch

昨天晚上买了iPod Touch 8G S$388,晚上手机没法照,上一张官网的图: 拿在手里的感觉很不错,超薄。终于可以用上传说中的多重触控了。音质?汗,我素木耳。 昨晚太赶,忘了贴膜,配套的套子也没有。今天再去森林看看。这些杂物好贵啊啊啊。