DataGridView and binding with BindingList<T> and DataSource

This is a discussion on DataGridView and binding with BindingList<T> and DataSource within the C# Programming forums, part of the General Programming Boards category; Hey there. I'm trying to simplify a bit my use of DataGridView (I am currently adding manually all the items ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73

    DataGridView and binding with BindingList<T> and DataSource

    Hey there. I'm trying to simplify a bit my use of DataGridView (I am currently adding manually all the items but I would like that to be done automatically).

    I've googled quite a bit and I haven't found the answer to my problem. Basically, I am almost copy-pasting this bit from tutorials (the only thing that changes is the name of my variables and text and such) and on my computer nothing shows up (well the dgv does show up, but empty).

    I tried using the simplest code possible and I still get nothing:
    Code:
            private void openAllToolStripMenuItem_Click(object sender, EventArgs e)
            {
                dataGridView2.AutoGenerateColumns = true;
                BindingList<Serie> ls = new BindingList<Serie>();
                ls.Add(new Serie("blablabla", "1234", 2));
                ls.Add(new Serie("blablabla2", "12345", 2));
                ls.Add(new Serie("blablabla3", "123456", 2));
                bs.DataSource = ls;
                dataGridView2.DataSource = bs;
                dataGridView2.Update();
            }
    I know the event is triggered because I only commented code that was working (but more verbose and manual) to try this method. I'm quite lost here. I've tried replacing BindingList<T> to List<T> (actually it's the other way around, I had List<T> first). I also tried using only strings instead of Serie object, but for some weird reason it would use the last column and output the number of characters in each strings :\ !

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Ok I've found the answer using google but I forgot to bookmark the reference for you guys but eh.

    Basically you have to set properties because DataGridView calls them in order and names the columns with the name of the properties so it's expected that passing a regular list as a data source will produce this result because the only property is Length or something like that (sorry, still very new to C#).

    Using the following code for my class Serie made the DataGridView able to display the information I wanted it to display:

    Code:
    public class Serie
    {
        private string  _Name;
        private string  _URL;
        private int     _Quantity;
        private bool    _Follow;
        private int     _QuantityNew;
    
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
    
        public string URL
        {
            get
            {
                return _URL;
            }
            set
            {
                _URL = value;
            }
        }
    
        public int Quantity
        {
            get
            {
                return _Quantity;
            }
            set
            {
                _Quantity = value;
            }
        }
    
        public bool Follow
        {
            get
            {
                return _Follow;
            }
            set
            {
                _Follow = value;
            }
        }
    
        public int QuantityNew
        {
            get
            {
                return _QuantityNew;
            }
            set
            {
                _QuantityNew = value;
            }
        }
    
    // irrelevant code
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. datagridview
    By darren78 in forum C# Programming
    Replies: 2
    Last Post: 11-16-2010, 04:43 PM
  2. Updating the Data on a Datagrid from the Datasource
    By AntV in forum C++ Programming
    Replies: 0
    Last Post: 07-22-2009, 12:15 PM
  3. DataGridView
    By sgh in forum C# Programming
    Replies: 1
    Last Post: 03-18-2009, 10:40 PM
  4. DataSource problem with C#
    By TheMajorRager in forum C# Programming
    Replies: 9
    Last Post: 10-14-2005, 03:38 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21