Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Location
    Ames, IA
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question [c#] Dynamically adding rows to DataGridView

    I am using a DataGridView in my app. How do I add a new row to the already existing dataset?

    I see tons of web control stuff, but nothing on window forms...

    I attempted this code
    dataGridView1.Rows.Add(new string());

    When I run this code, it is giving the following exception:

    exception: rows cannot be added programatically to the datagridview's collection when control is databound.

    Tell me there is a way to add rows to a DataGridView dynamically, since you can in C++.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Ames, IA
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Figured it out...

    Code:
    DataRow dr;
    DataSet ds; // this is bound to DataGridView
    
    // some logic setting up dataset and tables within
    
    dr = ds.Tables[0].NewRow(); // Table[0] of ds is the data I want to display
    dr["Column1"] = "Finally";
    dr["Column2"] = "Got";
    dr["Column3"] = "this";
    dr["Column4"] = "Working";
    
    // add row to dataset now
    ds.Tables[0].Rows.Add(dr);
    Last edited by Melon00; 06-15-2006 at 06:31 PM.

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The following link will help u

    http://csharp.net-informations.com/d...w-tutorial.htm

    rob.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •