Answered Prob. updating DataGridView with DataTable

Pyth007

Member
Joined
Nov 17, 2008
Messages
18
Programming Experience
5-10
This seems like a simple answer. I'm afraid that this question shows my lack of understanding about the DGV in general.

Basically, I have a form with a DGV on it that I want to use like a shopping cart... To do this, in the code I'm making a simple DataTable (_adoShoppingCart) with 3 columns: SystemName, SystemType, and Quantity (none have been established as a key... not sure if I need one). I have then set the DGV1.DataSource = myDataTable

When the user pushes a particular button, I want to add a new row to the DataTable and have that reflected in the DGV. The simplest option would be when the button pushed corresponds to a particular machine, so that both the SystemName and SystemType are already known, and the only variable that the user has to input would be the quantity:

VB.NET:
Private Sub btnAddPC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPC.Click
        Dim iNumPCs As Integer
        Dim strTemp As String = InputBox("Please enter in the number of Workstations:", "Number PCs")
        While Not Integer.TryParse(strTemp, iNumPCs)
            strTemp = InputBox("Incorrect value." & vbNewLine & "Please enter in an integer for the number of PCs in the order.", "Number PCs")
        End While

        Dim adoRow As System.Data.DataRow = Me._adoShoppingCart.NewRow()
        adoRow("SystemName") = "Workstation"
        adoRow("SystemType") = "1234-ABC"
        adoRow("Quantity") = iNumPCs
        Me._adoShoppingCart.ImportRow(adoRow)

        Me.DataGridView1.Update()
End Sub

However, there are other buttons that have more complexities, including having the user input additional data that is not stored in the datatable. Eventually I plan to store this data into a text-file, probably in XML format; hence there is no backend database that the DataTable is connecting to. This is also why I am not using a BindingSource, but rather am setting the DGV's DataSource directly to the DataTable itself.

My problem is that when I push the button, nothing happens -- I'm not even sure if I am adding the row correctly to the DataTable let alone why the DGV isn't being updated either. So my questions are two-fold: 1) whether I'm using the DataTable correctly in adding a row and 2) how to get that row to show up in the DGV.

*** Edit ***
I figured out an answer to my own question... Instead of using the "ImportRow" function of the DataTable, I just used the Add function for the Rows collection. I guess I'm not sure how the ImportRow works, but I got my code to work regardless.
 
Last edited:
This is also why I am not using a BindingSource, but rather am setting the DGV's DataSource directly to the DataTable itself.
Would help if you DID use a BS, and there is nothing stopping you. It may also help you to know that you are setting the datasource to the table but that is NOT what the DG is using. The DT has a .DefaultView that allows for sorting and filtering. That's what your DGV will bind itself to


I guess I'm not sure how the ImportRow works, but I got my code to work regardless.

I used .Add and I experience no issues.. Every row i .Add() shows up in the grid as expected. Import will take a copy of the row you pass.. It is intended for use in situations where you have a row that belongs to one table already. A row cannot belong to two different tables.
 
Back
Top