none
get old and new value from datagridview

    Question

  • i have a datagridview with four columns

    sku           qty     barcode         price

    2001           1      3333334          4.95
    2001           2      3333335          9.75
    2001           6      6663334         29.00
    2001          12      3442224         57.00

    i need to change the price of all qty, then i need to get the value of price and qty new and old.

    thanks,

    Sunday, July 19, 2009 6:36 PM

Answers

  • button2 will copy the original values into a new datatable.

    after changing the values, button3 will show the new values in another column

    Dim lstSKU As New List(Of String)
        Dim lstQTY As New List(Of Integer)
        Dim lstUPC As New List(Of Integer)
        Dim lstPRICE As New List(Of Decimal)
        Dim dtTest As New DataTable
        Dim dtTest2 As New DataTable
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            dtTest.Columns.Add("sku", GetType(String))
            dtTest.Columns.Add("qty", GetType(Integer))
            dtTest.Columns.Add("upc", GetType(Integer))
            dtTest.Columns.Add("price", GetType(Decimal))
    
            DataGridView1.DataSource = dtTest.DefaultView
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.RowCount - 1
                lstSKU.Add(DataGridView1.Rows(i).Cells(0).ToString)
                lstQTY.Add(CInt(DataGridView1.Rows(i).Cells(1).Value))
                lstUPC.Add(CInt(DataGridView1.Rows(i).Cells(2).Value))
                lstPRICE.Add(CDec(DataGridView1.Rows(i).Cells(3).Value))
            Next
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            dtTest2.Columns.Add("oldQty", GetType(Integer))
            dtTest2.Columns.Add("oldPrice", GetType(Decimal))
            dtTest2.Columns.Add("newQty", GetType(Integer))
            dtTest2.Columns.Add("newPrice", GetType(Decimal))
    
            For i As Integer = 0 To dtTest.Rows.Count - 1
                dtTest2.Rows.Add(dtTest.Rows(i).Item(1), dtTest.Rows(i).Item(3))
            Next
            DataGridView2.DataSource = dtTest2.DefaultView
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            For i As Integer = 0 To dtTest.Rows.Count - 1
                dtTest2.Rows(i).Item(2) = dtTest.Rows(i).Item(1)
                dtTest2.Rows(i).Item(3) = dtTest.Rows(i).Item(3)
            Next
            DataGridView2.DataSource = dtTest2.DefaultView
        End Sub
    • Marked as answer by Basheer EK Wednesday, July 22, 2009 4:08 PM
    Monday, July 20, 2009 6:06 PM

All replies

  • another possibility is to copy the datatable to another datatable before making any changes

    using the example from your other thread, button2 shows how to copy to another table and show the original in another datagridview. add some data to datagridview1, then press button2.

        Dim lstSKU As New List(Of String)
        Dim lstQTY As New List(Of Integer)
        Dim lstUPC As New List(Of Integer)
        Dim lstPRICE As New List(Of Decimal)
        Dim dtTest As New DataTable
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            dtTest.Columns.Add("sku", GetType(String))
            dtTest.Columns.Add("qty", GetType(Integer))
            dtTest.Columns.Add("upc", GetType(Integer))
            dtTest.Columns.Add("price", GetType(Decimal))
    
            DataGridView1.DataSource = dtTest.DefaultView
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.RowCount - 1
                lstSKU.Add(DataGridView1.Rows(i).Cells(0).ToString)
                lstQTY.Add(CInt(DataGridView1.Rows(i).Cells(1).Value))
                lstUPC.Add(CInt(DataGridView1.Rows(i).Cells(2).Value))
                lstPRICE.Add(CDec(DataGridView1.Rows(i).Cells(3).Value))
            Next
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim dtTest2 As New DataTable
            dtTest2 = dtTest.Copy
            DataGridView2.DataSource = dtTest2.DefaultView
        End Sub
    Monday, July 20, 2009 5:13 AM
  • hi arjun,

    thanks for ur response.

    I need just old and new value to store in another table.

    thanks,

    Monday, July 20, 2009 5:11 PM
  • button2 will copy the original values into a new datatable.

    after changing the values, button3 will show the new values in another column

    Dim lstSKU As New List(Of String)
        Dim lstQTY As New List(Of Integer)
        Dim lstUPC As New List(Of Integer)
        Dim lstPRICE As New List(Of Decimal)
        Dim dtTest As New DataTable
        Dim dtTest2 As New DataTable
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            dtTest.Columns.Add("sku", GetType(String))
            dtTest.Columns.Add("qty", GetType(Integer))
            dtTest.Columns.Add("upc", GetType(Integer))
            dtTest.Columns.Add("price", GetType(Decimal))
    
            DataGridView1.DataSource = dtTest.DefaultView
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.RowCount - 1
                lstSKU.Add(DataGridView1.Rows(i).Cells(0).ToString)
                lstQTY.Add(CInt(DataGridView1.Rows(i).Cells(1).Value))
                lstUPC.Add(CInt(DataGridView1.Rows(i).Cells(2).Value))
                lstPRICE.Add(CDec(DataGridView1.Rows(i).Cells(3).Value))
            Next
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            dtTest2.Columns.Add("oldQty", GetType(Integer))
            dtTest2.Columns.Add("oldPrice", GetType(Decimal))
            dtTest2.Columns.Add("newQty", GetType(Integer))
            dtTest2.Columns.Add("newPrice", GetType(Decimal))
    
            For i As Integer = 0 To dtTest.Rows.Count - 1
                dtTest2.Rows.Add(dtTest.Rows(i).Item(1), dtTest.Rows(i).Item(3))
            Next
            DataGridView2.DataSource = dtTest2.DefaultView
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            For i As Integer = 0 To dtTest.Rows.Count - 1
                dtTest2.Rows(i).Item(2) = dtTest.Rows(i).Item(1)
                dtTest2.Rows(i).Item(3) = dtTest.Rows(i).Item(3)
            Next
            DataGridView2.DataSource = dtTest2.DefaultView
        End Sub
    • Marked as answer by Basheer EK Wednesday, July 22, 2009 4:08 PM
    Monday, July 20, 2009 6:06 PM
  • hi jwavila,

    thanks for ur kind response.

    I don't want all data i need only the data of which row i am changing the price.  just old and new value to store in another table.

    thanks,

    Monday, July 20, 2009 7:22 PM