winForms + DataGridView binding to a List<T>
Asked Answered
F

4

34

I'm trying to bind a List<T> to a DataGridView control, and I'm not having any luck creating custom bindings.

I have tried:

gvProgramCode.DataBindings.Add(new Binding("Opcode",code,"Opcode"));

It throws an exception, saying that nothing was found by that property name.

The name of the column in question is "Opcode". The name of the property in the List<T> is Opcode.

ANSWER EDIT: the problem was that I did not have the bindable fields in my class as properties, just public fields...Apparently it doesn't reflect on fields, just properties.

Finedraw answered 24/9, 2008 at 2:40 Comment(3)
LOL that is just what I had added in my comments, glad you got your problem solved.Bhutan
+1 I've just had the same problem. Man that sucks balls!!!Colourable
You're a godsend. I've been beating my head against this problem for six hours straight, now. Tried everything I could think of, and everything I could think to Google. No luck. Finally found your post and, of course, I was in a hurry and made them fields, not properties.Chaffin
B
14

Is the property on the grid you are binding to Opcode as well?.. if you want to bind directly to List you would just DataSource = list. The databindings allows custom binding. are you trying to do something other than the datasource?

You are getting a bunch of empty rows? do the auto generated columns have names? Have you verified data is in the object (not just string.empty) ?

    class MyObject
    {
        public string Something { get; set; }
        public string Text { get; set; }
        public string Other { get; set; }
    }

    public Form1()
    {
        InitializeComponent();

        List<MyObject> myList = new List<MyObject>();

        for (int i = 0; i < 200; i++)
        {
            string num = i.ToString();
            myList.Add(new MyObject { Something = "Something " + num , Text = "Some Row " + num , Other = "Other " + num  });
        }

        dataGridView1.DataSource = myList;
    }

this should work fine...

Bhutan answered 24/9, 2008 at 2:49 Comment(5)
Yes. If I just do DataSource = list, I get a bunch of empty columns.Finedraw
So you just want to bind the grid to the opcode property in a list of custom objects?Bhutan
If what I asked is the cast then do... Grid.DataSource = MyList.Select(o => o.Opcode);Bhutan
There are three strings in class, Opcode is one of them. This class is the type for a List<T>, I want each string to be a column, and each list item to be a row...Finedraw
Another thing.. the strings in the object are properties and not just public fields right?.. if you could post the definition of the object it might help as well.Bhutan
S
5

I can't really tell what you're trying to do with the example you included, but binding to a generic list of objects is fairly straightforward if you just want to list the objects:

    private BindingSource _gridSource;

    private BindingSource GridSource
    {
        get
        {
            if (_gridSource == null)
                _gridSource = new BindingSource();
            return _gridSource;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<FluffyBunny> list = new List<FluffyBunny>();
        list.Add(new FluffyBunny { Color = "White", EarType = "Long", Name = "Stan" });
        list.Add(new FluffyBunny { Color = "Brown", EarType = "Medium", Name = "Mike" });
        list.Add(new FluffyBunny { Color = "Mottled", EarType = "Short", Name = "Torvald" });

        GridSource.DataSource = list;
        dataGridView1.Columns["EarType"].Visible = false; //Optionally hide a column
        dataGridView1.DataSource = GridSource;

    }

If you only want to display specific properties of the List's type you should be able to make the unwanted column(s) invisible.

Technically, you don't really need to create the BindingSource, but I find it's a whole lot easier when I'm doing updates or changes if I have it.

Hope this helps.

Sharlenesharline answered 24/9, 2008 at 2:59 Comment(2)
When binding directly to the List<T>, I get a bunch of empty rows, no data. The class in List<T> just contains 3 strings.Finedraw
Thanks Jared! Your comment about the BindingSource really helped me when I was trying to figure out how to create new rows when using a List<T>. Have a click!Mei
G
4

Had the same issue... I had a struct with public fields obviously. nothing in the grid. provided public getters, worked.

Guadalquivir answered 26/3, 2009 at 17:3 Comment(0)
G
4

Another solution I've found is to use the BindingList collection.



private void Form1_Load(object sender, EventArgs e)
{
   BindingList people= new BindingList {
    new Person {Name="John",Age=23},
    new Person {Name="Lucy",Age=16}
  };

   dataGridView1.DataSource= people;
}

It works fine for me,

Grosgrain answered 13/11, 2009 at 12:36 Comment(1)
Using BindingList<T> has saved my neck with trying to bind DataGridViews! Thank you.Zareba

© 2022 - 2024 — McMap. All rights reserved.