VS 2012 DataGridView Columns-VBForums
Results 1 to 4 of 4

Thread: DataGridView Columns

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,258

    DataGridView Columns

    Can I please have some help with some code.

    Code:
        Private Sub SetupDataGridViewColumns(DataGridView As DataGridView)
            Dim DataGridViewColumnCollection As New DataGridViewColumnCollection(DataGridView)
            Dim DataGridViewColumn As New DataGridViewColumn
            DataGridViewColumn.Name = "PersonID"
            DataGridViewColumn.HeaderText = "PersonID"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "First Name"
            DataGridViewColumn.HeaderText = "First Name"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "First Name"
            DataGridViewColumn.HeaderText = "First Name"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Last Name"
            DataGridViewColumn.HeaderText = "Last Name"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Address Line 1"
            DataGridViewColumn.HeaderText = "Address Line 1"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Address Line 2"
            DataGridViewColumn.HeaderText = "Address Line 2"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Address Line 3"
            DataGridViewColumn.HeaderText = "Address Line 3"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Mobile Phone"
            DataGridViewColumn.HeaderText = "Mobile Phone"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Home Phone"
            DataGridViewColumn.HeaderText = "Home Phone"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Date Modified"
            DataGridViewColumn.HeaderText = "Date Modified"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Person Category"
            DataGridViewColumn.HeaderText = "Person Category"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
            DataGridViewColumn.Name = "Comment"
            DataGridViewColumn.HeaderText = "Comment"
            DataGridViewColumnCollection.Add(DataGridViewColumn)
        End Sub
    I am getting the following error:

    Specified argument was out of the range of valid values.
    At line:

    Code:
    DataGridViewColumnCollection.Add(DataGridViewColumn)
    I have had a look online, with no result.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,325

    Re: DataGridView Columns

    Firstly, why are you creating a new DataGridViewColumnCollection when your DataGridView already has one, accessible via it's Columns property?

    Secondly, DataGridViewColumn is a class so you can't just create one instance and then add it to a collection multiple times and expect the collection to contain multiple columns. If you want multiple columns then you have to create multiple columns.

    Finally, you would never create an instance of the DataGridViewColumn class. It is really just a base class. If you want to display and optionally edit text in a DataGridView then you would use a DataGridViewTextBoxColumn.

    So, with all that in mind, your code should look something like this:
    vb.net Code:
    1. Private Sub SetupDataGridViewColumns(grid As DataGridView)
    2.     Dim columns = grid.Columns
    3.  
    4.     columns.Add("PersonID", "PersonID")
    5.     columns.Add("First Name", "First Name")
    6.     'etc.
    7. End Sub
    Note the rather better variable names too.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,258

    Re: DataGridView Columns

    How about for specifying the width for the column text? I had the same code as you above, but could not change the width, so I tried to add DataGridView items.... with no luck.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,325

    Re: DataGridView Columns

    The overload of the Add method that I used above returns the index of the column just added, so you can use that to get the column and then configure it, e.g.
    vb.net Code:
    1. Private Sub SetupDataGridViewColumns(grid As DataGridView)
    2.     Dim columns = grid.Columns
    3.  
    4.     columns(columns.Add("PersonID", "PersonID")).Width = 50
    5.     columns(columns.Add("First Name", "First Name")).Width = 100
    6.     'etc.
    7. End Sub
    Alternatively, you can create a DataGridViewTextBoxColumn yourself, configure it and then add it, e.g.
    vb.net Code:
    1. Private Sub SetupDataGridViewColumns(grid As DataGridView)
    2.     Dim columns = grid.Columns
    3.  
    4.     columns.Add(New DataGridViewTextBoxColumn With {.Name = "PersonID",
    5.                                                     .HeaderText = "PersonID",
    6.                                                     .Width = 50})
    7.     columns.Add(New DataGridViewTextBoxColumn With {.Name = "First Name",
    8.                                                     .HeaderText = "First Name",
    9.                                                     .Width = 100})
    10.     'etc.
    11. End Sub
    Actually, to make the code cleaner, I'd probably recommend breaking the addition of the column out into its own method:
    vb.net Code:
    1. Private Sub SetupDataGridViewColumns(grid As DataGridView)
    2.     Dim columns = grid.Columns
    3.  
    4.     SetupDataGridViewColumn(columns, "PersonID", 50)
    5.     SetupDataGridViewColumn(columns, "First Name", 100)
    6.     'etc.
    7. End Sub
    8.  
    9. Private Sub SetupDataGridViewColumn(columns As DataGridViewColumnCollection, text As String, width As Integer)
    10.     columns.Add(New DataGridViewTextBoxColumn With {.Name = text,
    11.                                                     .HeaderText = text,
    12.                                                     .Width = width})
    13.     'etc.
    14. End Sub

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.