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

asp.net Single Sign-On(SSO) from SAP

公司的主系统是SAP的东西,有的时候老板想要一些小的新功能或是某个部门需要一个自己使用的小系统又不想买SAP的东西(贵啊),只有自己写和使用一些第三方免费或是开源的系统,这个时候就难免涉及到 Single Sign-On 。而在asp.net 2.0下是非常容易实现的。有SAP提供的“Sapsecu.dll”,“sapssoext.dll”2个dll再加上由公司SAP系统提供的"verify.pse"就足够了。 第一步 复制Sapsecu.dll到system32目录,并使用Rersrv32注册。 第二步 在项目里添加对sapssoext.dll的引用,vs2005会自动生成Interop.SAPSSOEXT.dll来让.net使用。 第三步 在你项目的根目录创建sap目录,并将SAP系统那边提供的verify.pse复制过去。位置倒到不是一定要在这里,只要你找得到就好。 第四步 在SAP系统那边,需要生成一个类似:http://yourserver/login.aspx?sso=werwerwerwe的链接,当然具体的页面需要你和那边的管理员商量来决定。这个链接是有时效性的,我在做测试的时候他们给的测试链接通常只能用一天。现在我们要做到就是我们这边的编码,在login.aspx的page_load里面实现: protected void Page_Load(object sender, EventArgs e) {   string sso= Request.QueryString["sso"];   SAPSSOEXT.SSO2Ticket objSSO = new SAPSSOEXT.SSO2Ticket();   objSSO.CryptLib = "sapsecu.dll";   string strKeypath = Server.MapPath("~/sap/verify.pse");    //放在根目录比较容易找   object tt;   tt = objSSO.EvalLogonTicket(strTicket, strKeypath, String.Empty); ...

On Building

慢慢添东西,嘿嘿,放了个ico上去,美女哦,三国11的貂婵。 原计划是把整个css改为我自己设计的那个css的,仔细看了一下,我的和现在用这个根本没法比嘛。

GridView Sort (排序)

GridView本身有一个 Sort() 函数: public virtual void Sort (     string sortExpression,    SortDirection sortDirection) 使用上相当方便。 但在实际使用中,常用的是点击每一列的标题来排序,点击标题会触发: protected void GridView_Sorting(object sender, GridViewSortEventArgs e) 事件。从 GridViewSortEventArgs 中可以拿到 SortDirection 和 SortExpression 。但是 GridView.Sort() 本身也会触发 GridView_Sorting() ,如果在 GridView_Sorting() 中调用 GridView.Sort() 会无限的循环,直到堆栈溢出。 这样就只能使用 DataView.Sort 属性来排序,再Bind到GridView上。 示例: protected string m_sortDirection {   get   {     if (ViewState["m_SortDirection"] == null)   {     return String.Empty;   }   return (string)ViewState["m_SortDirection"];   }     set { ViewState["m_SortDirection"] = value; } } protected string m_sortExpression {   get { return (string)ViewState["m_SortExpression"]; }   set { ViewState["m_SortExpression"] = value; } } protecte...