Results 1 to 2 of 2

Thread: [2008] Adding row to unbound DataGridView

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    137

    [2008] Adding row to unbound DataGridView

    I am trying to display unbound data in a DataGridView. I set up columns already design-time. I am at a loss for creating code that adds a new row using the column schema that was created in the Designer?

    Something like this?

    vb Code:
    1. dim newrow as DataGridViewRow
    2. newrow.cells.item("name").value = "text here"
    3. datagridview1.rows.add(newrow)

    I get an error saying the 'name' column was not found?
    Jesse Bunch
    www.getbunch.com/
    If I have helped you, please rate my posts!

    Unless otherwise indicated, I am using the following Products:

    Visual Studio .NET 2010
    .NET Framework 4.0

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

    Re: [2008] Adding row to unbound DataGridView

    You should call datagridview1.Rows.Add() to generate the row. That way it will have the correct number of columns and each will be of the appropriate type. If you just create a DataGridViewRow yourself then how would it know what columns the grid has?

    Add will return the index of the row, so you can then access it from the Rows collection by that index to set its fields.
    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