Results 1 to 2 of 2

Thread: DataGridView DataSource

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    440

    DataGridView DataSource

    I have row set in my IDE. At run time I want to load a set number of new rows. 25 below but that might become a variable.

    Each row as a ComboBox. How can I set the row's datasource to a list at runtime/


    Code:
    For i As Integer = 0 To 25 - 1
        Dim DGVR = New DataGridViewRow()
        gameDataGrid.Rows.Add()
    Next

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

    Re: DataGridView DataSource

    Firstly, you don't need a loop. The .Rows.Add method you're calling allows you to specify a number of rows to add, so you can add them all in one call.

    As for the data source for the drop-down list, you can set the DisplayMember, ValueMember and DataSource properties of the column exactly as you would for a regular ComboBox. Those property values will then be propagated to every cell in the column and any editing contro9ls that are embedded in those cells. If you need different values for different rows, you can set those same properties on individual cells explicitly, which will then be propagated to any editing controls. To access row, simply index or enumerate the Rows collection. To access a cell within a row, index its Cells collection, or you can index the grid itself by column and row.

    Note that, when you get a cell, you'll only get a DataGridViewCell reference. You'll need to cast as type DataGridViewComboBoxCell in order to access type-specific properties. The same goes for the column if you index the Columns collection, i.e. DataGridViewColumn to DataGridViewComboBoxColumn, although you will have a filed of the correct type if you created the columns in the designer.
    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