none
How to refresh a datagridview after adding a new row

    Question

  • I am experiencing abnormal behaviour of datagridview such as after I add a row manually, row added programmatically will not appear on datagridview. I think I need to refresh the datagridview after adding a new row to the datasource programmatically. The problem is that I don't know how to do it so that the datagridview will show updated data from the dataset. Any comment is very much appreciated. :-)

    Here is my code:
        Private Function itemDetailTableForCreation() As DataTable
            Dim t As New DataTable
            t.Columns.Add("ItemID")
            t.Columns.Add("Item")
            t.Columns.Add("Description")
            t.Columns.Add("Quantity")
            t.Columns.Add("Unit")
            t.Columns.Add("UnitPrice")
            t.Columns.Add("Amount")
            t.Columns.Add("UnitCost")
            t.Columns.Add("TotalCost")
            Return t
        End Function

        ... Set datasource
        dgItemDetail.DataSource = itemDetailTableForCreation()
       
        ... Add new row
        Dim newRow As DataRow = _dtDataTable.NewRow

                        newRow("ItemID") = s.sItemID
                        newRow("Item") = s.sItemName
                        newRow("Description") = s.sDescription
                        newRow("Quantity") = 1
                        _dtDataTable.Rows.Add(newRow)


    Thursday, November 08, 2007 3:42 PM

Answers

  • Hi Timmy,

     

    I think the issue is your datagridview (dgItemDetail) is not data bound to the same object referenced by your _dtDataTable variable. This is a little sublte.

     

    In this line of code you bind the grid to a new object instance of a datatable returned by the function. This is not the same as _dtDataTable in your code:

    dgItemDetail.DataSource = itemDetailTableForCreation()

     

    So the trick is to make sure you add new rows and make changes to the same instance of the datatable object you data bind to in grid.DataSource = YourTable.  Once you do this, your _dtDataTable object will fire change events and the grid will refresh automatically as a result. 

     

    Here's how I would do it - changes in bold:

     

        Dim WithEvents _dtDataTable As DataTable

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'create a new DataTable and hold on to a reference
            _dtDataTable = Me.itemDetailTableForCreation

     

            '... Set datasource
            dgItemDetail.DataSource = _dtDataTable

     

            '... Add new row
            Dim newRow As DataRow = _dtDataTable.NewRow

     

            'assign some sample data
            'TODO: wire up to real data
            newRow("ItemID") = "sample id"
            newRow("Item") = "sample name"
            newRow("Description") = "sample description"
            newRow("Quantity") = 1

     

            _dtDataTable.Rows.Add(newRow) 'should fire event and update databindings

        End Sub

     

        Private Function itemDetailTableForCreation() As DataTable
            Dim t As New DataTable
            t.Columns.Add("ItemID")
            t.Columns.Add("Item")
            t.Columns.Add("Description")
            t.Columns.Add("Quantity")
            t.Columns.Add("Unit")
            t.Columns.Add("UnitPrice")
            t.Columns.Add("Amount")
            t.Columns.Add("UnitCost")
            t.Columns.Add("TotalCost")
            Return t
        End Function

     

    Does this do the trick for you?

     

    Best,

    Paul - VB Team

    Friday, November 09, 2007 8:30 PM
    Moderator

All replies

  • Hi Timmy,

     

    I think the issue is your datagridview (dgItemDetail) is not data bound to the same object referenced by your _dtDataTable variable. This is a little sublte.

     

    In this line of code you bind the grid to a new object instance of a datatable returned by the function. This is not the same as _dtDataTable in your code:

    dgItemDetail.DataSource = itemDetailTableForCreation()

     

    So the trick is to make sure you add new rows and make changes to the same instance of the datatable object you data bind to in grid.DataSource = YourTable.  Once you do this, your _dtDataTable object will fire change events and the grid will refresh automatically as a result. 

     

    Here's how I would do it - changes in bold:

     

        Dim WithEvents _dtDataTable As DataTable

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'create a new DataTable and hold on to a reference
            _dtDataTable = Me.itemDetailTableForCreation

     

            '... Set datasource
            dgItemDetail.DataSource = _dtDataTable

     

            '... Add new row
            Dim newRow As DataRow = _dtDataTable.NewRow

     

            'assign some sample data
            'TODO: wire up to real data
            newRow("ItemID") = "sample id"
            newRow("Item") = "sample name"
            newRow("Description") = "sample description"
            newRow("Quantity") = 1

     

            _dtDataTable.Rows.Add(newRow) 'should fire event and update databindings

        End Sub

     

        Private Function itemDetailTableForCreation() As DataTable
            Dim t As New DataTable
            t.Columns.Add("ItemID")
            t.Columns.Add("Item")
            t.Columns.Add("Description")
            t.Columns.Add("Quantity")
            t.Columns.Add("Unit")
            t.Columns.Add("UnitPrice")
            t.Columns.Add("Amount")
            t.Columns.Add("UnitCost")
            t.Columns.Add("TotalCost")
            Return t
        End Function

     

    Does this do the trick for you?

     

    Best,

    Paul - VB Team

    Friday, November 09, 2007 8:30 PM
    Moderator
  • On a related note, we have some nice features like the strongly typed dataset and dataset designer/wizards that make it really easy to design the columns for datatables, set up data bindings, etc.  It will reduce a lot of code in the sample above.

     

    If you're interested to check it out, I suggest watching some of these How To - data series videos that my team built:

    http://msdn2.microsoft.com/en-us/vbasic/bb466226.aspx?wt.slv=topsectionsee#formsoverdata

     

    Best,

    Paul

     

    Friday, November 09, 2007 8:38 PM
    Moderator