2008-09-29 76 views
0

我有一个problem部分解决。为了快速解释它:我有一个网格绑定到一个需要序列化的复杂对象。当从序列化构建对象时,像网格上的事件不会刷新表格显示。有人告诉我,一旦反序列化就重建这个事件,它起作用!但是刷新网格的事件似乎根本不会触发。C#ResetBinding翻转DataGridView *更新示例*

我不得不从我的复杂对象中建立一个事件,告诉我内部发生了一些变化。从这个事件中,我添加了这个代码:

this.bindingSource1.ResetBindings(false); 

问题是网格翻转和用户没有一个良好的感觉(行上下,比停动起来)。

如何在不进行此类翻转的情况下重置绑定? 我该如何解决original problem? (这将自动解决所有问题)。

更新

这里是做同样的行为的一个例子:

创建一个类:

[Serializable()] 
class BOClient : INotifyPropertyChanged, IDataErrorInfo 
{ 
    private string name; 
    private int len; 
    public string Name 
    { 
     get { return name; } 
     set { name = value; 
     this.len = name.Length; 
     if (this.PropertyChanged !=null) 
      this.PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
     } 
    } 

    public int Len 
    { 
     get { return this.len; } 
    } 

    public BOClient(string name) 
    { 
     this.Name = name; 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    #region IDataErrorInfo Members 

    public string Error 
    { 
     get { return ""; } 
    } 

    public string this[string columnName] 
    { 
     get { return ""; } 
    } 

    #endregion 
} 

现在,创建一个BindingSource的形式称之为 “bindingSource1” 和去使用类作为数据源。创建一个网格并将网格绑定到bindingsource1。

在这种形式使用该代码的负载:

private void Form1_Load(object sender, EventArgs e) 
    { 
     BindingList<BOClient> listClient = new BindingList<BOClient>(); 
     listClient.Add(new BOClient("P1")); 
     listClient.Add(new BOClient("P2")); 
     listClient.Add(new BOClient("P3")); 

     //using (MemoryStream mem = new MemoryStream()) 
     //{ 
     // BinaryFormatter b1 = new BinaryFormatter(); 

     // try 
     // { 
     //  b1.Serialize(mem, listClient); 
     // } 
     // catch (Exception ez) 
     // { 
     //  MessageBox.Show(ez.Message); 
     // } 




     // BinaryFormatter b2 = new BinaryFormatter(); 

     // try 
     // { 
     //  mem.Position = 0; 
     //  listClient = (BindingList<BOClient>)b2.Deserialize(mem); 
     // } 
     // catch (Exception ez) 
     // { 
     //  MessageBox.Show(ez.Message); 
     // } 

     //} 


     this.bindingSource1.DataSource = listClient; 
    } 

我把序列化过程中的评论,因为它似乎做同样怪异的行为没有它...现在启动应用程序。更改客户的名称。 “新名称”的例子“p1”,然后单击更改后的单元格。你会看到“len”列不变。但是如果您单击具有len的单元格,您将看到数字更改为正确的值。

任何人有一个想法,为什么?

回答

0

我已经解决了这个问题,通过在BindingList(通过继承)中添加一个方法[OnDeserialization],我添加了在OnListChange上添加事件的代码。这样当1个属性发生变化时,整条线都会刷新。