我有两个datagridview,并且我试图通过单击按钮的复选框将datagridview1中的值插入datagridview2中。
For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
Dim c As Boolean
c = DataGridView1.Rows(i).Cells(0).Value
If c = True Then
With DataGridView1.Rows(i)
DataGridView2.Rows.Insert(0, .Cells(0).Value, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value, .Cells(4).Value, .Cells(5).Value)
End With
End If
Next
我的代码每次插入都会插入数据。我想防止在datagridview2上插入相同的数据。如您所见,每次尝试插入相同的复选框时,都可以在下图中看到,我可以多次添加它。 非常感谢。
这就是我填充datagridview1的方式。
Sub dgv1_SubjectList()
query = "SELECT subject_id AS '#', subject_name AS 'Descriptive Title', subject_units AS 'Units', sem AS 'Semester', year_level AS 'Year Level' " & _
" FROM subject WHERE course = '" & cmb_Course.Text & "' AND CURRICULUM = '" & curriculum & "' "
da = New MySqlDataAdapter(query, myconn)
da.Fill(ds, "subject")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
在datagridview2上,我直接在datagridview2中添加空列。
谢谢。抱歉,不能及时更新。
答案 0 :(得分:0)
我尽力再现您的环境。
DataGridView1.AllowUserToAddRows = True
DataGridView2.AllowUserToAddRows = True
DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, True, "Row1Col1", "Row1Col2", "Row1Col3", "Row1Col4", "Row1Col5", "Row1Col6")
DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, True, "Row2Col1", "Row2Col2", "Row2Col3", "Row2Col4", "Row2Col5", "Row2Col6")
DataGridView2.Rows.Insert(DataGridView2.Rows.Count - 1, False, "Row1Col1", "Row1Col2", "Row1Col3", "Row1Col4", "Row1Col5", "Row1Col6")
For i As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
If DataGridView1.Rows(i).Cells(0).Value Then
Dim AlreadyInGrid As Boolean = False
Dim ColumnsCount As Integer = DataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Displayed)
For j As Integer = DataGridView1.Rows.Count - 2 To 0 Step -1
For k As Integer = 1 To ColumnsCount - 1
If DataGridView1.Rows(i).Cells(k).Value <> DataGridView2.Rows(j).Cells(k).Value Then Exit For
AlreadyInGrid = (k = ColumnsCount - 1)
Next
If AlreadyInGrid Then Exit For
Next
If Not AlreadyInGrid Then
With DataGridView1.Rows(i)
DataGridView2.Rows.Insert(DataGridView2.Rows.Count - 1, False, .Cells(1).Value, .Cells(2).Value, .Cells(3).Value, .Cells(4).Value, .Cells(5).Value, .Cells(6).Value)
End With
End If
End If
Next
答案 1 :(得分:0)
在我看来,您似乎想移动该行,如果那样的话,您不必担心检查该行是否已存在,因为它无法尝试再次添加。
Public Class Form1
Dim ds As New DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Begin sample setup
ds.Tables.Add(New DataTable With {.TableName = "Table1"})
With ds.Tables(0)
.Columns.Add("Select", GetType(Boolean))
.Columns.Add("ID", GetType(Integer))
.Columns.Add("Column2", GetType(String))
.Rows.Add(False, 1, "test1")
.Rows.Add(False, 2, "test2")
.Rows.Add(False, 3, "test3")
.Rows.Add(False, 4, "test4")
End With
Dim dt As DataTable = ds.Tables(0).Clone
dt.TableName = "Table2"
ds.Tables.Add(dt)
DataGridView1.DataSource = ds.Tables(0)
DataGridView2.DataSource = ds.Tables(1)
'end sample setup
End Sub
Private Sub ButtonAddToDT2_Click(sender As Object, e As EventArgs) Handles ButtonAddToDT2.Click
Validate()
Dim SelRows() As DataRow = ds.Tables(0).Select("Select=True")
For Each DtRow As DataRow In SelRows
ds.Tables(1).ImportRow(DtRow)
ds.Tables(0).Rows.Remove(DtRow)
Next
End Sub
End Class
如果您不是要删除该行,而您确实想查看datagridview2中是否存在该行,请告诉我们,我们可以对此代码进行一些小的修改。