简体   繁体   English

vb.net DataGridview比较

[英]vb.net DataGridview compare

I am having 2 DataGridView in my vb.net form. 我在vb.net表单中有2个DataGridView。 datagridview1 has a DataSource with sql server DB. datagridview1有一个带有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. 预期的行为如果我双击datagridview1,则所选值应显示在datagridview2中,而该值应在datagridview1中删除。 This should repeat whenever I double click my datagridview1 hence my datagridview2 value should not be in datagridview1 enter code here 每当我双击datagridview1时都应重复此操作,因此我的datagridview2值不应位于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? 双击单元格时,是否要将整行从DGV1移动到DGV2?

Should data from DGV1 go to specific row in DGV2? 应该从DGV1数据去具体排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. 如果未在运行时创建DGV2,则可以在加载表单时添加列。

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". 当您单击datagridview1中的行时,将表中的“组”字段从“ 1”更新为“ 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. 第一个网格的数据源将仅显示组“ 1”的表数据,而第二个网格的数据源将仅显示组“ 2”的表数据。单击将进行更新,网格应刷新,并且您应该会得到您想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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