Results 1 to 7 of 7

Thread: [RESOLVED] [2.0] windows datagrid binding using arraylist or collectionbase class

  1. #1

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Resolved [RESOLVED] [2.0] windows datagrid binding using arraylist or collectionbase class

    Hi,

    Has anyone using this kind of binding before?

    Thanks,

    Popskie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] windows datagrid binding using arraylist or collectionbase class

    You simply assign the collection to the DataSource property, just as you would with a DataTable. The objects in the collection must have a public property to correspond to each column you want to display in the grid.

    Also, if you're using .NET 2.0 then I strongly suggest that you use the DataGridView in preference to the DataGrid. If you actually are using a DataGridView then I'd ask that you be more careful with your terminology because, while the answer is the same in this case, the two controls are quite different.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: [2.0] windows datagrid binding using arraylist or collectionbase class

    thats what I did in my code but I dont know what is lacking or error in my code below JM.
    VB Code:
    1. private void button1_Click(object sender, System.EventArgs e)
    2.         {
    3.             henTable ht = new henTable();
    4.             ht.Add("1","First Data","Description");        
    5.             dg1.DataSource = ht;   
    6.        
    7.         }      
    8.     }
    9.     class henTable : CollectionBase
    10.     {
    11.         private container cnt;     
    12.         public void Add(string key,string objn1,string objn2)
    13.         {
    14.             cnt = new container();
    15.             cnt.Key = key;
    16.             cnt.Object1 = objn1;
    17.             cnt.Object2 = objn2;
    18.             this.List.Add(cnt);
    19.         }
    20.        
    21.  
    22.     }
    23.     struct container
    24.     {
    25.         public string Key;
    26.         public string Object1;
    27.         public string Object2;
    28.     }

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] windows datagrid binding using arraylist or collectionbase class

    Note the wording of my previous post:
    The objects in the collection must have a public property to correspond to each column you want to display in the grid.
    You cannot bind fields, only properties. You should make your fields private and expose them via public properties, as is generally good practice anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: [2.0] windows datagrid binding using arraylist or collectionbase class

    thanks jms. It work great now.But what is the difference if I not expose and expose using public properties.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] windows datagrid binding using arraylist or collectionbase class

    Properties exist because they offer certain advantages, basically by combining the external behaviour of a field and the internal behaviour of methods. If you want to know more about those advantages then I suggest an expedition to MSDN.

    As for data binding, when you bind an object such as a collection to a control it is the objects property descriptors that actually get bound. Your collection will automatically generate property descriptors for the properties of the objects it contains. You could add property descriptors manually for fields if you want to, but why would you?

    Take, for example, when you bind a DataTable to a grid. The rows of the table don't have any properties or fields that correspond to the columns of the table. The DataTable class automatically generates property descriptors for each column, thus allowing the data from those columns to be displayed in a bound control.

    The .NET Framework does a lot of work for you behind the scenes. Data binding is something that can be taken for granted but think about how much code you'd have to write to get the same level of functionality without it. Property descriptors are a part of that and the fact that they are generated automatically for properties is something to be thankful for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: [2.0] windows datagrid binding using arraylist or collectionbase class

    OK JM thank so much for this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width