【发布时间】:2024-01-15 10:43:01
【问题描述】:
我有两个 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 中添加空列。
谢谢。抱歉更新晚了。
【问题讨论】:
-
请说明您是如何设置datagridview的数据源,或者首先添加行
-
@Mr.Tripodi 为最新的更新。我如何填充我的数据网格视图
-
看起来你可以通过从datagridview1中删除行来解决问题,然后你不能将它添加到datagridview2。我还假设这就是您首先以相反顺序遍历行的原因,您打算删除该行吗?
标签: vb.net checkbox datagridview