none
Update data in the DataTable RRS feed

  • Question

  • Hi,

          How to update the data in the datatable not using the dataadapter and command object.
          Here i attached my code. The dtStockBalance table contains 2 records like
           CategoryCode    BalanceStock
              1                  50
              2                  20
                  I need to update the BalanceStock of CategoryCode=2 in the datatable  how could i do
             can anyone solve my problem.

                

     Private Function CreateStockBalanceDataTable() As DataTable
            dtStockBalance = New DataTable("StockBalance")
            Try
                dtStockBalance.Columns.Add(New DataColumn("CategoryCode", Type.GetType("System.String")))
                dtStockBalance.Columns.Add(New DataColumn("BalanceStock", Type.GetType("System.String")))

                Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
                PrimaryKeyColumns(0) = dtStockBalance.Columns("CategoryCode")
                dtStockBalance.PrimaryKey = PrimaryKeyColumns
                Return dtStockBalance
            Catch ex As Exception
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                Return dtStockBalance
            End Try
        End Function


         Private Sub AddBalanceStockMember(ByVal dtStockBalance As DataTable)
            Try
              
                    Dim NewStockMemberRow As DataRow = dtStockBalance.NewRow()
                    NewStockMemberRow("CategoryCode") = ddlItemName.SelectedValue.ToString
                    Dim intQunatity As Integer = CType(txtQuantity.Text.Trim(), Integer)
                    Dim intStockCount As Integer
                    Dim intUnitOfMeasure As Integer
                    intUnitOfMeasure = CType(ddlUnitofMeasure.SelectedValue, Integer)
                    intStockCount = intQunatity * intUnitOfMeasure
                    NewStockMemberRow("BalanceStock") = intStockCount
                    dtStockBalance.Rows.Add(NewStockMemberRow)
                    dtStockBalance.AcceptChanges()
                   
                    dgViewStock.DataSource = dtStockBalance
             
            Catch ex As Exception
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            End Try
        End Sub

       
    Thanks and Regards
    brite
    Monday, April 20, 2009 6:16 PM

Answers

  • If you need to update existing row in DataTable, first you need to locate it. One way is to use index of the row and assign values to specific column, for example

    dtStockBalance.Rows(1)("BalanceStock")=100

    above method usually hard to use since usually location of specific row is not known in advance

    Second way is to create DataView based on DataTable and assign RowFilter to filter your rows and locate specific row. DataView does not do physical filtering of the rows and does not make copy of the data rows from the DataTable, but uses direct reference to each row, so changes in DataView will be reflected in DataTable. You could should look something like

    Dim myView as DataView = New DataView(dtStockBalance)

    DataView.RowFilter = "CategoryCode  = 2"
    DataView.Item(0)("BalanceStock")=100

    One more method is to use Find method of the DataTable to locate specific row, but Find will form only on primary key field(s) defined in DataTable
    Val Mazur (MVP) http://www.xporttools.net
    Tuesday, April 21, 2009 10:29 AM
    Moderator