将选中的行显示到另一个 DataGridView 中 [英] Show checked rows into another DataGridView

查看:40
本文介绍了将选中的行显示到另一个 DataGridView 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView 填充了一个 DataTable.
我添加了一个 CheckBoxColumn 来选择一些行.

I have a DataGridView populated with a DataTable.
I added a CheckBoxColumn to select some rows.

我的目标是只将选定的行显示到另一个 DataGridView

My Goal is to show only selected rows into another DataGridView

我尝试使用 Select 并将结果添加到新的 DataTable 来完成此操作,但是 Select 不起作用,因为 CheckBoxColumn 丢失.

I tryed to accomplish this using Select and adding results to a new DataTable but Select doesn't work because CheckBoxColumn is missing.

这是我用来填充第一个 DataGridView 并添加一个 CheckBoxColumn 的代码:

Here's the code I used to fill 1st DataGridViewand add a CheckBoxColumn:

Dim chk0 As New DataGridViewCheckBoxColumn()
With chk0
    .AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
    .HeaderText = "Sel"
    .Name = "Sel"
End With
With Me.DataGridView1
    .Columns.Clear()
    .DataSource = DT_Events
    .Columns.Insert(0, chk0)
    .Columns("Event").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
End With

然后,在检查了一些行之后,我尝试将选定的行显示到另一个 DataGridView 中:

Then, after checking some rows, I tryed to show the selected ones into another DataGridView:

Using DT_Checked As DataTable = CType(Me.DataGridView1.DataSource, DataTable).Select("Sel = 1").CopyToDataTable
    If DT_Checked.Rows.Count > 0 Then
        With Me.DataGridView2
            .Visible = True
            .DataSource = DT_Checked
        End With
    Else
        MsgBox("No rows to show", MsgBoxStyle.Critical, "Error")
    End If
End Using

我在没有 Select 的情况下尝试了此代码,但它没有显示 CheckBoxColumn.
我该如何解决?
否则我该怎么做?

I tryed this code without Select and it doesn't show me CheckBoxColumn.
How can I fix?
How can I do it otherwise?

推荐答案

将行复制到新的 DataTable 是一种浪费,因为两者之间唯一真正的区别在于是否某些 Boolean> 值为 True 或 False.由于您绑定到 DataTable,您可以简单地更改每个 DGV 中显示的视图.

Copying rows to a new DataTable is wasteful because the only real difference between the two is whether some Boolean value is True or False. Since you are binding to a DataTable, you can simply change the view displayed in each DGV.

标题中表达的概念,显示选中的行...不同于复制行到代码尝试执行的另一个控件.这将展示如何使用一个 DataSource 来显示每个 DGV 中的一些行.

The concept expressed in the title, show checked rows... is different than copying rows to another control as the code tries to do. This will show how to use one DataSource to display some rows in each DGV.

如果您的数据来自数据库,您可以在 SQL 中添加一列:

If your data comes from a database, you can add a column in the SQL:

' Access version
Dim sql = "SELECT a, b, c, False AS Selected FROM SAMPLE"

这将为所有行添加一个初始化为 False 的布尔列,并将在 DGV 中显示为 CheckBox.

This will add a Boolean Column initialized to False for all rows and will display in the DGV as a CheckBox.

如果数据以其他方式进入 DataTable,请手动添加列:

dtSample.Columns.Add("Selected", GetType(Boolean))
dtSample.Columns("Selected").DefaultValue = False

' we need to loop and set a value
' if you manually add a column
For Each r As DataRow In dtSample.Rows
    r("Selected") = False
Next

要根据该值在一个网格(或列表框或组合)或另一个网格中显示行,此代码将使用 2 个 DataViews.如果您正在使用视图,您经常希望在使用时更改 RowFilter,因此请为表单添加一些全局内容:

To display rows in one grid (or listbox or combo) or the other based on that value, this code will use 2 DataViews. If you are using views, you often want to change the RowFilter as you go, so make a few things global to the form:

Private dtSample As DataTable          ' base table for BOTH DGVs
Private dvSource As DataView           ' ALL or Selected = False view
Private dvDest As DataView             ' Selected only
...
' build datatable and add the Selected Row (if needed)
...
' create Source DV as Selected = False
dvSource = New DataView(dtSample, "Selected=False", "", DataViewRowState.CurrentRows)

' create SELECTED DV as Selected = True
dvSelected = New DataView(dtSample, "Selected=True", "",DataViewRowState.CurrentRows)

dgv1.DataSource = dvSource
dgv2.DataSource = dvSelected 

dvSource 是可选的.如果您希望所有行都显示在第一个 DGV 中,您可以使用 DataView(根据问题似乎就是这种情况).

dvSource is optional. If you want all rows to show in the first DGV, you that DataView (this appears to be the case as per the question).

出于说明目的,此设置是为了在 DGV1 中检查项目时,它们消失"(因为它们不再符合 Selected = False 标准),并自动出现在 DGV2 中(因为,现在他们确实符合那个标准.结果:

For illustrative purposes, this is set up so that as items are checked in DGV1 they "disappear" from it (because they no longer meet the Selected = False criteria), and automagically appear in DGV2 (because, now they do meet that criteria. Results:

底部 DGV 中未选中/未选中的行将返回顶部.

Rows unchecked/unselected in the bottom DGV will skitter back to the top one.

经济实惠.您不仅根本不需要运行任何代码来添加或移动一行到第二个 DGV,而且您不需要制作 DataRows 的副本和一个新的DataTable 这样做.通过 TaskManager(粗略的,但具有指示性),内存量与 Selected Rows 的变化大致相同;手动复制它们时,当您复制包含相同数据的 DataRows 时,它会慢慢爬起来.

It is economical. Not only do you not have to run any code at all to appear to add or move a row to the second DGV, but you are not making copies of DataRows and a new DataTable to do so. Going by TaskManager (rough, but indicative), the amount of memory stays about the same as Selected Rows change; when manually copying them, it slowly creeps up as you make copies of DataRows containing the same identical data.

这篇关于将选中的行显示到另一个 DataGridView 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆