Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridView doesn't keep column order

$
0
0
I have a DataGridView on my form that I bind to a BindingSource, which in turn is bound to BindingList<MyStruct>.

MyStruct is a custom struct with three public properties (say A, B, C).

I set up the columns for my DataGridView in code, by adding the appropriate columns in the order in want them (A, B, C). However, at runtime, the grid displays them in the order A, C, B.

Is this a bug? Did I forget anything?

struct MyStruct
{
    private string _a;
    private string _b;
    private int _c;

    public MyStruct(string a, string b, int c) 
    {
        _a = a;
        _b = b;
        _c = c;
    }

    public string A { get { return _a; } }
    public string B { get { return _b; } }
    public int C { get { return _c; } }
}


// in the form constructor:

BindingList<MyStruct> list = new BindingList<MyStruct>();
UpdateList(list, someCondition);

bindingSource1.DataSource = list;

dataGridView1.Columns.Add("column0", "A");
dataGridView1.Columns.Add("column1", "B");
dataGridView1.Columns.Add("column2", "C");
dataGridView1.Columns["column0"].DataPropertyName = "A";
dataGridView1.Columns["column1"].DataPropertyName = "B";
dataGridView1.Columns["column2"].DataPropertyName = "C";

dataGridView1.DataSource = bindingSource1;

Viewing all articles
Browse latest Browse all 2535

Trending Articles