简体   繁体   中英

vb.net DataGridview compare

I am having 2 DataGridView in my vb.net form. datagridview1 has a DataSource with sql server DB.

Expected behavior If I double click my datagridview1 my selected value should be displayed in datagridview2 and that value should be removed in datagridview1. This should repeat whenever I double click my datagridview1 hence my datagridview2 value should not be in datagridview1 enter code here

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        For i As Integer = 0 To Me.DataGridView1.SelectedCells.Count - 1
            iRowIndex = Me.DataGridView1.SelectedCells.Item(i).RowIndex
            icolindex = Me.DataGridView1.SelectedCells.Item(i).ColumnIndex
            cell_value = DataGridView1.Rows(iRowIndex).Cells(icolindex).Value
            selected_row_pk = DataGridView1.Rows(iRowIndex).Cells(0).Value
        Next
        If (DataGridView2.RowCount = 0 & DataGridView2.ColumnCount = 0) Then
            Dim dt1 As New DataTable
            DataGridView2.Rows.Add(selected_row_pk, cell_value)
            frst_rem_CELL_value = cell_value
            frst_rem_ROW_PK_value = selected_row_pk
            dt1 = DataGridView1.DataSource
            dt1.Rows(DataGridView1.CurrentRow.Index).Delete()
            dt1.AcceptChanges()
            DataGridView1.DataSource = dt1
            dtt = DataGridView1.DataSource
            a = 2
        Else

            If a = 2 Then
                dtt.Rows.Add(frst_rem_ROW_PK_value, frst_rem_CELL_value)
                dtt.AcceptChanges()
                DataGridView1.DataSource = dtt
                dtt5.Columns.Add("id", GetType(Integer))
                dtt5.Columns.Add("expence_date", GetType(String))
                dt_for_grid2.Columns.Add("id", GetType(Integer))
                dt_for_grid2.Columns.Add("expence_date", GetType(String))
                a = 1
            End If

            If CLng(counter) Mod 2 > 0 Then
                frst_rem_CELL_value1 = cell_value
                frst_rem_ROW_PK_value1 = selected_row_pk
                dt_for_grid2.Rows(DataGridView1.CurrentRow.Index).Delete()
                dt_for_grid2.AcceptChanges()
                dt_for_grid2.Rows.Add(frst_rem_ROW_PK_value2, frst_rem_CELL_value2)
                dt_for_grid2.AcceptChanges()
                DataGridView2.Rows.Add(selected_row_pk, cell_value)
                Dim rowIndex As Integer = DataGridView2.CurrentCell.RowIndex
                DataGridView2.Rows.RemoveAt(rowIndex)
                Dim distinctDT As DataTable = dt_for_grid2.DefaultView.ToTable(True, "id", "expence_date")
                distinctDT.AcceptChanges()
                DataGridView1.DataSource = distinctDT
            Else

                frst_rem_CELL_value2 = cell_value
                frst_rem_ROW_PK_value2 = selected_row_pk
                Dim rowIndex As Integer = DataGridView2.CurrentCell.RowIndex
                dt_for_grid1 = DataGridView1.DataSource
                dt_for_grid1.Rows(DataGridView1.CurrentRow.Index).Delete()
                dt_for_grid1.AcceptChanges()
                dt_for_grid2 = dt_for_grid1
                dtt5.Rows.Add(selected_row_pk, cell_value)
                dtt5.AcceptChanges()
                dt_for_grid1.AcceptChanges()
                DataGridView4.DataSource = dtt5
                DataGridView1.DataSource = dt_for_grid2
                DataGridView2.Rows.Add(selected_row_pk, cell_value)
                DataGridView2.Rows.RemoveAt(rowIndex)
            End If
            counter = counter + 1
        End If
        DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
    End Sub

When you double click a cell, do you want to move whole row from DGV1 to DGV2?

Should data from DGV1 go to specific row in DGV2?

Private Sub DGV1_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV1.CellContentDoubleClick
    DGV2.Rows.Add(DGV1(0,e.RowIndex).Value,DGV1(1,e.RowIndex).Value) 'adds data from selected DGV1 row to DGV2
    DGV1.Rows.RemoveAt(e.RowIndex) 'I guess this is different with databound datagridview.
End Sub

If DGV2 is not created at runtime, you can add columns when form is loaded.

I guess I would approach it differently, if I understand it correctly. The data table should have an additional "group" field. When you click on the row in datagridview1, update the "group" field in the table from "1" to "2". The data source for the first grid would only show table data for group "1", and the data source for the second grid would only show table data for group "2" The click would do the update, the grids should refresh, and you should get the results you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM