Results 1 to 29 of 29

Thread: [RESOLVED] [2008] How to add new row to datagridview?

  1. #1

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Resolved [RESOLVED] [2008] How to add new row to datagridview?

    Can anyone helpme In adding new row to DataGridView?

    I tried this but it didn't work
    vb Code:
    1. Dim _row As DataGridViewRow
    2.         _row.Cells(0).Value = "Value1"
    3.         _row.Cells(1).Value = "Value2"
    4.         DataGridView1.Rows.Add(_row)

    it just give one Warning in the Error list saying
    Variable '_row' is used before it has been assigned a value. A null reference exception could result at runtime.
    and after dubug it highlight this part
    vb Code:
    1. _row.Cells(0).Value = "Value1"

    and type this
    Object reference not set to an instance of an object.
    help plz
    thnx
    Last edited by mahammad(); Aug 31st, 2008 at 07:24 AM.

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

    Re: [2008] How to add new row to datagridview?

    Firstly, the warning should give you a pretty good clue. It's saying that you haven't assigned a value to the variable and a NullReferenceException could result. When you run the code a NullReferenceException does result, so the solution would probably be to assign a value to the variable.

    As for the NullReferenceException itself, run your code again. When the exception is thrown you need to LOOK at the Exception Assistant window and READ what it says. It tells you EXACTLY what to do. The Exception Assistant contains various links. Generally you click on links and they take you to some useful information. That might be the case here.
    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
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Exclamation Re: [2008] How to add new row to datagridview?

    First : thanks for answer
    second : I don't know What to do

    this is the a screenshot for the error


    but how to add new row to a DGV normally

    please give me an example
    thanks in advance
    Attached Images Attached Images  
    Last edited by mahammad(); Aug 31st, 2008 at 10:16 AM.

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] How to add new row to datagridview?

    Code:
    Dim _row As New DataGridViewRow

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

    Re: [2008] How to add new row to datagridview?

    I specifically said in my last post that the Exception Assistant window tells you what to do and provides links to more information. Did you click a single link on that window? The very first link in the "Troubleshooting tips" section tells you what you need to do and if you'd clicked that link you'd have been taken to a Help topic that explained it. You don't have to be an experienced programmer to read and to click a link.
    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

  6. #6

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Angry Re: [2008] How to add new row to datagridview?

    Quote Originally Posted by jmcilhinney
    I specifically said in my last post that the Exception Assistant window tells you what to do and provides links to more information. Did you click a single link on that window? The very first link in the "Troubleshooting tips" section tells you what you need to do and if you'd clicked that link you'd have been taken to a Help topic that explained it. You don't have to be an experienced programmer to read and to click a link.
    jmcilhinney I am sorry , but can you show me by code (I am sure you know)
    really I couldn't find how to do this

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

    Re: [2008] How to add new row to datagridview?

    First up, MaximilianMayrhofer has already posted the exact code. Secondly, I've told you exactly where to click on the very window that you posted yourself to find the information you need. If you're really not capable of following those extremely clear instructions then I would suggest that perhaps software development is not for you. The problems you face, and the instructions to fix them, will get many orders of magnitude harder than this so what's happened here doesn't bode well.
    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

  8. #8

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to add new row to datagridview?

    jmcilhinney , can you give me an example plz

    and the code that MaximilianMayrhofer posted have a new problem
    I think , the problem not from the row adding code it's from the cell , when I typed
    vb Code:
    1. _row.cells(1).value = "Value2"
    it says that the index par. is greater than the collection value
    I mean that there is no "cell1" so I must add it first , right?
    I think that this code not for adding it just for editing the cell value
    so please how to add cell

    many thanks jmcihinney
    thanks MaximilianMayrhofer

    thnx

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

    Re: [2008] How to add new row to datagridview?

    The number of cells in the row is determined by the number of columns in the grid. If you want to be able to set the Value of two cells in a row that you add then you have to have added at least two columns to the grid in the first place.
    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

  10. #10

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to add new row to datagridview?

    yeah I have the columns and I could add a row
    but only the first row I used this
    vb Code:
    1. DataGridView1.Rows(0).Cells(0).Value = "value1"
    2.         DataGridView1.Rows(0).Cells(0).Value = "value2"
    and it works but this didn't work
    vb Code:
    1. Dim _row As New DataGridViewRow
    2.         _row.Cells(0).Value = "value1"
    3.         _row.Cells(1).Value = "value2"
    4.         DataGridView1.Rows.Add(_row)
    it highlight the second line and say
    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
    so What to do now?
    thanks

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

    Re: [2008] How to add new row to datagridview?

    Ah, sorry. You need to add the row to the grid before you set the cell values. It's not until the row is added to the grid that it knows how many columns it will have.
    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

  12. #12

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Question Re: [2008] How to add new row to datagridview?

    OK I tried this
    vb Code:
    1. Dim _row As New DataGridViewRow
    2. DataGridView1.Rows.Add(_row)
    3. _row.Cells(0).Value = "value1"
    4. _row.Cells(1).Value = "value2"
    but it highlight the second line and say
    Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
    What does it mean?

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

    Re: [2008] How to add new row to datagridview?

    It means that if you have set the grid's DataSource, i.e. bound data to it, then you can't add a row directly to the grid. The grid displays whatever is in its data source. If you want to add a row to the grid you need to add an item to the data source.
    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

  14. #14

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to add new row to datagridview?

    How to add an Item to the datasource?
    and What's the Item? row?

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

    Re: [2008] How to add new row to datagridview?

    What's the data source?
    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

  16. #16

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to add new row to datagridview?

    Why are you asking? don't know !!
    I don't know how to explain what is the data source!
    Give me a sample please...

    -Hope it's the last
    -Thanks

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

    Re: [2008] How to add new row to datagridview?

    Open your eyes. Either the grid's DataSource is set in the designer or it's set in code. Either way, it would take magic for me to be able to tell you what is. It's your project.
    Last edited by jmcilhinney; Sep 2nd, 2008 at 08:11 PM.
    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

  18. #18

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to add new row to datagridview?

    Quote Originally Posted by jmcilhinney
    Open your eyes. Either the grid's DataSource is set in the designer or it's set in code. Either way, it would take magic for me to be able to tell you what is. It's your project.
    the datasource is the project!
    I know that the data source is a file , the .mdf I think?
    But what I will do when I know what's the datasource?

    I saw I sample before which add columns and rows to Datagridview but can't remember , I just know that it was by the DGV control only , there are not a database connection

    jmcilhinney , What to do now?

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

    Re: [2008] How to add new row to datagridview?

    Ugh! Open your form. Select the grid. Open the Properties window. Look at the DataSource property.

    Eitjer you want the grid bound or you don't. If you do then you have to add new rows to the data source, not the grid. It's that simple. If you want to be able to add new rows to the grid directly then you can't bind it, so you need to clear the DataSource property. That said, I don't think you'd have bound it in the first place if you didn't want it bound. If you didn't even know that you had bound it then I'm guessing that it was done automatically by the system because you dragged a table from the Data Sources window onto the form. That's just a guess though, because you haven't really explained.
    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

  20. #20

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to add new row to datagridview?

    You have the right to intolerance
    But why all that I don't know what do you mean.
    Finally , I clear the datasource property and tried this code and...worked!
    vb Code:
    1. DataGridView1.Columns.Add("First", "firstcolumn")
    2. DataGridView1.Columns.Add("Second", "Secondcolumn")
    3. DataGridView1.Rows.Add(1)
    4. DataGridView1.Rows(0).Cells(0).Value = "row1 column1"
    5. DataGridView1.Rows(0).Cells(1).Value = "row1 column2"
    6. DataGridView1.Rows(1).Cells(0).Value = "row2 column1"
    7. DataGridView1.Rows(1).Cells(1).Value = "row2 column2"
    This is that I want from the first post

    It worked but is there anything wrong that must be done other way?
    THANKS
    Last edited by mahammad(); Sep 3rd, 2008 at 09:02 AM.

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

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    There's a few little things to note there:

    1. Adding the columns in code is certainly legitimate but in most cases you'd add the columns in the designer.

    2. You're actually adding three rows there. The third line adds 1 row and the fourth line adds two more.

    3. You normally would write out that long line of code to refer to the cells each time. If you're going to refer to multiple cells in one row then you'd normally use a With block, for brevity and efficiency:
    vb.net Code:
    1. With DataGridView1.Rows(0).Cells
    2.     .Item(0).Value = "row1 column1"
    3.     .Item(1).Value = "row1 column2"
    4. End With
    That way you only have to retrieve the Cells collection once in total instead of once for each cell.
    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

  22. #22

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Thumbs up Re: [RESOLVED] [2008] How to add new row to datagridview?

    Quote Originally Posted by jmcilhinney
    1. Adding the columns in code is certainly legitimate but in most cases you'd add the columns in the designer.
    so adding columns in designer is better?

    Quote Originally Posted by jmcilhinney
    2. You're actually adding three rows there. The third line adds 1 row and the fourth line adds two more.
    yeah i was thinking that rows.add(2) will add row index 2 not two rows , I have edited it

    Quote Originally Posted by jmcilhinney
    3. You normally would write out that long line of code to refer to the cells each time. If you're going to refer to multiple cells in one row then you'd normally use a With block, for brevity and efficiency:
    vb.net Code:
    1. With DataGridView1.Rows(0).Cells
    2.     .Item(0).Value = "row1 column1"
    3.     .Item(1).Value = "row1 column2"
    4. End With
    That way you only have to retrieve the Cells collection once in total instead of once for each cell.
    what's the difference between
    vb Code:
    1. datagridview1.rows(0).cells(0).value = "row1 column1"
    and
    vb Code:
    1. datagridview1.rows(0).cells.item(0).value = "row1 column1"


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

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    1. Did you add the grid to the form in the designer or in code? In the designer, right? So why would you not add the columns in the designer too? If you know all the information you need to create them at design time then create them at design time. The only reason to create the columns in code is if you don't know something you need to create them until run time.

    3. There is no difference. Just like all collections, the Cells property of a DataGridViewRow has an Item property that is declared as the Default property. Default properties are ALWAYS indexed. Default properties can be accessed explicitly or implicitly. The idea is that they allow objects that can be indexed to be treated as though they were arrays. The full code for the above would be:
    vb.net Code:
    1. DataGridView1.Rows.Item(0).Cells.Item(0).Value = "row1 column1"
    Note that the Rows property is also a collection, so you must use its Item property to get an item too. In both cases Item is the default property, so you can access it implicitly. This allows you to treat both the Rows collection and the Cells collection as though they were arrays. It also shortens the code so most people would use the short form:
    vb.net Code:
    1. DataGridView1.Rows(0).Cells(0).Value = "row1 column1"
    There is absolutely no difference in the result of compiling those two bits of code.

    Now, the reason that I used the Item property previously is simple. In your original code you were going to the DataGridView, getting its Rows collection, getting the first row, getting the Cells collection of that row, getting the first cell, then finally setting its Value. In the very next line you repeat the first four steps all over again, then get the second cell and then set its Value. If you already had a reference to the Cells collection then why bother going through those same four steps to get it again? What if you had 1000 cells to change? My code avoids repeating those steps by using a With block. The With block gets a reference to a specific object, in this case the Cells collection of the desired row, and allows you to use that reference multiple times within the block. The thing is, you have to access members of that object with a dot inside the block. For that reason the Item property must be used explicitly. You can't do this:
    vb.net Code:
    1. With DataGridView1.Rows(0).Cells
    2.     (0).Value = "row1 column1"
    3.     (1).Value = "row1 column2"
    4. End With
    because that's a syntax error. The compiler doesn't know that the indexes at the beginning of those lines relate to the Cells collection because there's no leading dot. You can't do this either:
    vb.net Code:
    1. With DataGridView1.Rows(0).Cells
    2.     .(0).Value = "row1 column1"
    3.     .(1).Value = "row1 column2"
    4. End With
    because that's also a syntax error. You need the leading dot to tell the compiler that you're referring to a member of the object referred to by your With statement and you need to follow that dot by a member, therefore you must use the Item property explicitly:
    vb.net Code:
    1. With DataGridView1.Rows(0).Cells
    2.     .Item(0).Value = "row1 column1"
    3.     .Item(1).Value = "row1 column2"
    4. End With
    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

  24. #24

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Thumbs up Re: [RESOLVED] [2008] How to add new row to datagridview?

    1.Yeah the DGV will not be visable and adding rows will be until the run time

    3.Understood

    thanks

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

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    Quote Originally Posted by mahammad()
    1.Yeah the DGV will not be visable and adding rows will be until the run time
    Of course the rows won't be added until run time. I'm talking about adding the columns at design time.
    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

  26. #26

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    ok but how to add columns at design time. I know only if there is a database
    Last edited by mahammad(); Sep 5th, 2008 at 09:28 AM.

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

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    Take a look. It's not that hard to work out.
    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

  28. #28

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    yeah I found it

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

    Re: [RESOLVED] [2008] How to add new row to datagridview?

    Good. I recommend in future to always look first, ask questions later. It really is a far better way to learn, so you'll be the one to benefit. It certainly was true, and is true, for me.
    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