Results 1 to 5 of 5

Thread: Datagridview Binding

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Datagridview Binding

    Hi Guys:
    Can I Buy array to Datagridview? How do I do it?

    Thanks

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Re: Datagridview Binding

    Quote Originally Posted by simon ong
    Hi Guys:
    Can I Buy array to Datagridview? How do I do it?

    Thanks
    Sorry, typo error, I meant Bind Array .

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

    Re: Datagridview Binding

    Yes you can, and you do it in exactly the same way as you bind anything else to a DataGridView: assign it to the DataSource property.

    That said, you can bind a 1D array to the grid and each property of the elements becomes a column in the grid. You can NOT bind a 2D array to a DGV.
    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    155

    Re: Datagridview Binding

    Quote Originally Posted by jmcilhinney
    Yes you can, and you do it in exactly the same way as you bind anything else to a DataGridView: assign it to the DataSource property.

    That said, you can bind a 1D array to the grid and each property of the elements becomes a column in the grid. You can NOT bind a 2D array to a DGV.

    Code:
     string[] WeekDays = new string[] { "Sun", "Sat", "Mon", "Tue", "Wed", "Thu", "Fri" };
    
                this.dataGridView1.DataSource = WeekDays;
    Nothing showed on the grid!

    Can your help?

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

    Re: Datagridview Binding

    Like I said, when you bind an array to a grid the columns of the grid will correspond to the properties of elements. The actual string values do not correspond to any property of the elements of array so they are not displayed in a column. If you had an array of Day objects and each of those had a property of type String that returned the name of the day, THOSE values would be displayed. I can't think of any useful purpose for binding the array that you've shown to a grid. A combo box column in a grid maybe, but not a grid itself.
    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

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