Search code examples
c#winformsdevexpress

Dynamically created DevExpress DataGridView contains no data after setting DataSource


I have created a GridControl and GridView with the designer and set the DataSource and everything works fine. However when I create in code a new GridControl and a new GridView assigned to that GridControl and set the DataSource property correctly, then the GridView holds no data and RowCount returns 0. Any ideas?

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        gridControl1.DataSource = StraddleDataHandler.Data;

        GridControl g1 = new GridControl();

        g1.DataSource = StraddleDataHandler.Data;

        GridView v1 = new GridView(gridControl1);

        Console.WriteLine(v1.RowCount);
    }
}

Everything works fine when I set the DataSource property in my manually created grid control in designer, any ideas?


Solution

  • You need to set the BindingContext for the GridControl before it will generate.

    g1.BindingContext = this.BindingContext;