Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BindingSource/DataGridView interaction

I cannot understand why my DataGridView keeps on being empty (no rows, no autogenerated columns):

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();

where MyObject is defined as

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();
    }

}

In addition if I explicitily set columns as below, rows are added to the grid but they are all empty

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);
like image 578
Mauro Avatar asked Jun 18 '12 07:06

Mauro


1 Answers

In a Winforms project, try this code (kind of simulator of what you have posted & it works !) & then map / debug to your existing code to narrow down the problem:-

    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 equivalent :-

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

So, to Debug your code by mapping to the above one, you may remove these bits -

 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 in the 1st trial -

 this.bindingSource1.DataSource = bList;
 this.dataGridView1.DataSource = bindingSource1;
like image 85
Angshuman Agarwal Avatar answered Sep 28 '22 03:09

Angshuman Agarwal