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:
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.
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: