所以我终于能够将选定的行复制到第二个datagridview的新数据表中。但是我必须选择每一行。我有一个复选框列,我想改为选择每一行,但是我不确定如何使它符合我在这里所做的。我将发布我提到的两件事。
Dim chk As New DataGridViewCheckBoxColumn()
DataGridView1.Columns.Add(chk)
chk.HeaderText = "Cart"
chk.Name = "checkBoxColumnName"
上面的代码用于datagridview1上的复选框。下面是我的datagridview2代码。再次,这项工作通过在datagridview1上为datagridview2选择了我想要的所有行,但我喜欢我的复选框列,只需选择下面代码的行。
Dim Cart As New DataTable
Cart.Columns.Add("Part Number")
Cart.Columns.Add("Part Description")
Cart.Columns.Add("Quan. Avail.")
Cart.Columns.Add("U of M")
Cart.Columns.Add("Min")
Cart.Columns.Add("Vendor")
Cart.Columns.Add("Vendor Part Number")
Cart.Columns.Add("Location")
Dim drow As DataRow
For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
drow = Cart.NewRow
drow("Part Number") = DataGridView1.SelectedRows(i).Cells("MTI_PART_NO").Value
drow("Part Description") = DataGridView1.SelectedRows(i).Cells("DESCRIPTION").Value
drow("Quan. Avail.") = DataGridView1.SelectedRows(i).Cells("STOCKROOM_QTY").Value
drow("U of M") = DataGridView1.SelectedRows(i).Cells("UOM").Value
drow("Min") = DataGridView1.SelectedRows(i).Cells("MIN_QTY").Value
drow("Vendor") = DataGridView1.SelectedRows(i).Cells("VendorID").Value
drow("Vendor Part Number") = DataGridView1.SelectedRows(i).Cells("VendorNO").Value
drow("Location") = DataGridView1.SelectedRows(i).Cells("HOME_LOCATION").Value
Cart.Rows.Add(drow)
Next
DataGridView2.DataSource = Cart.DefaultView
我试过这个,但似乎没有用,我不确定我是否做得对。
For Each row As DataGridViewRow In DataGridView1.Rows
Dim chkCell As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell)
If Convert.ToBoolean(chkCell.Value) = True Then
Dim drow As DataRow
For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
drow = Cart.NewRow
drow("Part Number") = DataGridView1.SelectedRows(i).Cells("MTI_PART_NO").Value
drow("Part Description") = DataGridView1.SelectedRows(i).Cells("DESCRIPTION").Value
drow("Quan. Avail.") = DataGridView1.SelectedRows(i).Cells("STOCKROOM_QTY").Value
drow("U of M") = DataGridView1.SelectedRows(i).Cells("UOM").Value
drow("Min") = DataGridView1.SelectedRows(i).Cells("MIN_QTY").Value
drow("Vendor") = DataGridView1.SelectedRows(i).Cells("VendorID").Value
drow("Vendor Part Number") = DataGridView1.SelectedRows(i).Cells("VendorNO").Value
drow("Location") = DataGridView1.SelectedRows(i).Cells("HOME_LOCATION").Value
Cart.Rows.Add(drow)
Next
End If
Next
DataGridView2.DataSource = Cart.DefaultView