none
DatagridView insert/Add RRS feed

  • Question

  • I have a xml string that comes from a cache db (with 10rows and 6cols).
    Grid gets loaded correctly with 10rows and 6 cols for the first 
    time.

    How to insert a row to the datagirdview rather than overwriting what exisits already. Please suggest what needs to be done in this case. 

    Dim dsXMLData As System.Data.DataSet = New System.Data.DataSet("data")
    Dim dr As New System.IO.StringReader(XML)
    Dim DT As New DataTable
    ' create datasource from XML document
    dsXMLData.ReadXml(dr)
    ' assign data to grid
    dgvMainGrid.DataSource = dsXMLData.Tables(0)
    dgvMainGrid.Refresh()

    Many thanks.




    • Edited by PeterWalls Tuesday, January 29, 2013 1:58 PM
    Tuesday, January 29, 2013 11:15 AM

Answers

  • Sorry Peter, I assumed you wanted a blank row. My bad. You can either use the code you already posted, but don't add the row to DT ... instead import it to your existing DataSet/DataTable (in your original post, DT is not associated with dsXMLData at all, that's why it doesn't show up in your grid):

    ' don't do this 'DT.Rows.Add(dr)

    ' do this instead dsXMLData.Tables(0).ImportRow(dr)

    or change what I first posted to this:

    DataRow dr = dsXMLData.Tables(0).NewRow()
    dr.ItemArray = New Object() {"TEST",V2","V3","V4","V5","V6"}
    dsXMLData.Tables(0)Rows.Add(dr)


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked as answer by PeterWalls Tuesday, January 29, 2013 4:42 PM
    Tuesday, January 29, 2013 3:35 PM
  • Do this after you fill dsXMLData from the StringReader!

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked as answer by PeterWalls Tuesday, January 29, 2013 4:42 PM
    Tuesday, January 29, 2013 4:13 PM

All replies

  • Try this:

    dsXMLData.Tables(0)Rows.Add(dsXMLData.Tables(0).NewRow())


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Tuesday, January 29, 2013 2:53 PM
  • Dim dr As DataRow = DT.NewRow
    DT.Columns.Add("C1", Type.GetType("System.String"))
    DT.Columns.Add("C2", Type.GetType("System.String"))
    DT.Columns.Add("C3", Type.GetType("System.String"))
    DT.Columns.Add("C4", Type.GetType("System.String"))
    DT.Columns.Add("C5", Type.GetType("System.String"))
    DT.Columns.Add("C6", Type.GetType("System.String"))
    dr.ItemArray = New Object() {"TEST",V2","V3","V4","V5","V6"}
    DT.Rows.Add(dr)
    dgvMain.Refresh()
    Used the above to test the application before using the xml string from the DB, but the inserted row doesn't seem to be populated in the grid. Unsure whether I'm missing an important step in between!
    Tuesday, January 29, 2013 3:16 PM
  • thanks BonnieB, New 'blank' Row seem to be created when I'm adding a binding source and calling NewRow(). But cant make the data bound to the dataset/binding source !
    Tuesday, January 29, 2013 3:18 PM
  • Sorry Peter, I assumed you wanted a blank row. My bad. You can either use the code you already posted, but don't add the row to DT ... instead import it to your existing DataSet/DataTable (in your original post, DT is not associated with dsXMLData at all, that's why it doesn't show up in your grid):

    ' don't do this 'DT.Rows.Add(dr)

    ' do this instead dsXMLData.Tables(0).ImportRow(dr)

    or change what I first posted to this:

    DataRow dr = dsXMLData.Tables(0).NewRow()
    dr.ItemArray = New Object() {"TEST",V2","V3","V4","V5","V6"}
    dsXMLData.Tables(0)Rows.Add(dr)


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked as answer by PeterWalls Tuesday, January 29, 2013 4:42 PM
    Tuesday, January 29, 2013 3:35 PM
  • Cannot find table 0. Indexoutofrangeexception occurred while using
    dsXMLData.Tables(0).Rows.Add(dr) and dsXMLData.Tables(0).ImportRow(dr)
     !
    Tuesday, January 29, 2013 4:01 PM
  • Do this after you fill dsXMLData from the StringReader!

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked as answer by PeterWalls Tuesday, January 29, 2013 4:42 PM
    Tuesday, January 29, 2013 4:13 PM