DataGridView Add Row
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: DataGridView Add Row

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    DataGridView Add Row

    Hi, I have DataGridView and I have a Add New Row Button and a Delete Row Button. What is the code to add a new row in the datagridview? Also the code to delete a row from the datagridview?

    Note: I do not want to enable the AllowUserToAddRows and AllowUserToDeleteRows properties of the datagridview because I want to do it programatically.

    Thanks!

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: DataGridView Add Row

    Is your grid bound to a data source? If so then you should be adding and deleting rows in that data source, not the grid. If the grid is unbound then you call the Add and Remove methods of the grid's Rows collection.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: DataGridView Add Row

    Adding a row to datagridview with textBox columns
    Code:
            Dim dgvRow As New DataGridViewRow
            Dim dgvCell As DataGridViewCell
    
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "anis"
            dgvRow.Cells.Add(dgvCell)
    
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "khan"
            dgvRow.Cells.Add(dgvCell)
    
            DataGridView1.Rows.Add(dgvRow)

  4. #4
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: DataGridView Add Row

    Remove and RemoveAt methods of datagridview can be used to remove a row
    Code:
      DataGridView1.Rows.RemoveAt(index)
    Adding a row to the dataTable if datagridview is bound to it
    Code:
            Dim row As DataRow = dt.NewRow
            row("Col1") = "anis"
            row("Col2") = "khan"
            dt.Rows.Add(row)
    Similarly to remove row from datatable use remove and removeAt
    Code:
    dt.Rows.RemoveAt(index)

  5. #5
    Join Date
    Aug 2009
    Posts
    1

    Re: DataGridView Add Row

    i tried ur code. it is working. but when i add 4th column in the same row then it gives me an error. pl help me.

  6. #6
    Join Date
    Jan 2010
    Posts
    1

    Re: DataGridView Add Row

    http://msdn.microsoft.com/en-us/library/ddtce152.aspx

    http://msdn.microsoft.com/en-us/library/wc06dx4f.aspx

    DataGridView1.ColumnCount = 4
    looks like the kind of syntax that maybe useful to you.

  7. #7
    Join Date
    Mar 2010
    Posts
    1

    Re: DataGridView Add Row

    I tried that code and following error message came up: "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."

    I use VB 2009 express am i doing something wrong?
    I am a complete amateur, if you did not understood that yet.

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: DataGridView Add Row

    if you have a databound grid, you should add a row to datasource to see an extra row in the grid.
    If you want a different kind of row (say a totals row) you should look for FooterRow...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    May 2010
    Posts
    1

    Re: DataGridView Add Row

    Dear aniskhan

    when you talk about datagridview is bounded , I try your code :

    Dim row As DataRow = dt.NewRow
    row("Col1") = "anis"
    row("Col2") = "khan"
    dt.Rows.Add(row)

    But it does not work , can you explain what you mean by dt

    actully it's give mistake in " dt.NewRow " & " dt.Rows.Add(row) "

  10. #10
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,784

    Re: DataGridView Add Row

    You realize the post you are asking about is 4 years old. You may not get a response from the poster.

    However if you look at the code it is pretty clear that dt is the datatable that the grid is bound to.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Aug 2010
    Posts
    1

    Re: DataGridView Add Row

    yo can find some useful datagridvire help from the following url

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

    gever.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center