【问题标题】:BindingSource/DataGridView interactionBindingSource/DataGridView 交互
【发布时间】:2012-06-18 07:59:19
【问题描述】:

我不明白为什么我的 DataGridView 一直为空(没有行,没有自动生成的列):

BindingList<MyObject> bList = new BindingList<MyObject>();
 fileStream.Position = 0;
 MyObject.Deserialize(fileStream).ForEach(
     j => bList.Add(j));

 this.bindingSource1.SuspendBinding();

 this.dataGridView1.Columns.Clear();
 this.dataGridView1.AutoGenerateColumns = true;
 this.dataGridView1.Enabled = false;
 this.dataGridView1.Invalidate();
 this.bindingSource1.DataSource = bList;
 this.dataGridView1.DataSource = bindingSource1;
 this.bindingSource1.ResumeBinding();
 this.dataGridView1.Enabled = true;
 this.dataGridView1.Refresh();

其中 MyObject 被定义为

public class MyObject
{
    public DateTime CreationDate;
    public string CreationId;

    public static List<MyObject> Deserialize(Stream s)
    {
        XDocument xml = XDocument.Load(s);

        var ps = from p in xml
                      .Descendants("p")
                      .Descendants("object")
                  select
                      new MyObject
                      {
                          CreationId = p.Attribute("creationid").Value
                      };

        return ps.ToList();
    }

}

此外,如果我明确设置列如下,行会添加到网格中,但它们都是空的

DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn();
            dc.DataPropertyName = "CreationDate";
            dc.HeaderText = "CreationDate";
            dc.Name = "CreationDate";
            dc.Visible = true;
            dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns.Add(dc);

            dc = new DataGridViewTextBoxColumn();
            dc.DataPropertyName = "CreationId";
            dc.HeaderText = "CreationId";
            dc.Name = "CreationId";
            dc.Visible = true;
            dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns.Add(dc);

【问题讨论】:

  • 快速观察 - 1. 尝试将“creationid”重命名为 CreationId,就像网格视图的 DataPropertyCreationDate 在对象中丢失。
  • 以上注释仅在Autogeneratecolumn is set to false-->场景下有效
  • 错误。 creationid 就是这样写在 xml 中的。你的解决方案行不通。

标签: c# winforms datagridview


【解决方案1】:

在 Winforms 项目中,尝试此代码(您发布的模拟器类型 & it works !)然后映射/调试到您现有的代码以缩小问题范围:-

    DataGridView dgv = new DataGridView();
    //Note: AutogenerateColumns is true by default
    BindingSource bs = new BindingSource();
    BindingList<Customer> bList = new BindingList<Customer>();

    // Fill bList with Customers
    bList.Add(new Customer(){Name="John"});

    bs.DataSource = bList;
    dgv.DataSource = bs;

    this.Controls.Add(dgv);

MyObject 等效 :-

public class Customer
    {
        public string Name { get; set; }
    }

所以,to Debug your code 通过映射到上面的,你可以删除这些位 -

 this.bindingSource1.SuspendBinding();
 this.dataGridView1.Columns.Clear();
 this.dataGridView1.AutoGenerateColumns = true;
 this.dataGridView1.Enabled = false;
 this.dataGridView1.Invalidate();
 this.bindingSource1.ResumeBinding();
 this.dataGridView1.Enabled = true;
 this.dataGridView1.Refresh();

&just have this一审中-

 this.bindingSource1.DataSource = bList;
 this.dataGridView1.DataSource = bindingSource1;

【讨论】:

  • 你应该更好地指出我错过了 {get; set;} 但 +1 以便快速回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2018-05-13
  • 2018-04-15
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多