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

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

问题描述

我有一个 DataGridView 填充了一个 DataTable

我添加了一个 CheckBoxColumn 选择一些行。



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



我尝试使用选择并将结果添加到新的 DataTable 选择不起作用,因为 CheckBoxColumn 缺少。 p>

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

  Dim chk0 As New DataGridViewCheckBoxColumn()
带有chk0
.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
.HeaderText =Sel
.Name =Sel
结束与
与Me.DataGridView1
.Columns.Clear()
.DataSource = DT_Events
.Columns.Insert(0,chk0)
.Columns(Event)。AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
E nd

然后,在检查了一些行后,我尝试将所选的一个显示为另一个 DataGridView

 使用DT_Checked As DataTable = CType(Me.DataGridView1.DataSource,DataTable )。选择(Sel = 1)。CopyToDataTable 
如果DT_Checked.Rows.Count> 0然后
与Me.DataGridView2
.Visible = True
.DataSource = DT_Checked
结束
Else
MsgBox(没有行显示, MsgBoxStyle.Critical,Error)
结束如果
结束使用

我尝试这个代码没有选择,它不显示我 CheckBoxColumn

我怎么能修复?

否则可以这样做?

解决方案

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



标题中, 显示已检查行... 行不同于代码尝试的另一个控件去做。这将显示如何使用一个 DataSource 在每个DGV中显示一些行。



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

 '访问版本
Dim sql =SELECT a,b,c,False AS Selected FROM SAMPLE

这将添加一个布尔值列为所有行初始化为False,并将在DGV中显示为复选框



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

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

'我们需要循环并设置一个值
',如果您手动添加列
对于每个r作为DataRow在dtSample.Rows
r(Selected)= False
下一个

要在一个网格(或列表框或组合)中显示行或基于该值显示行,此代码w不适用2 DataViews 。如果您正在使用视图,则您经常想要更改 RowFilter ,因此请在表单中填写全球数据:

 私有dtSample作为DataTable'BOTH DGVs的基表
私有dvSource As DataView'ALL或Selected = False视图
私有dvDest作为DataView'仅选择
...
'构建数据并添加Selected Row(如果需要)
...
'创建源DV作为Selected = False
dvSource = New DataView (dtSample,Selected = False,,DataViewRowState.CurrentRows)

'创建SELECTED DV作为Selected = True
dvSelected =新建DataView(dtSample,Selected = True ,DataViewRowState.CurrentRows)

dgv1.DataSource = dvSource
dgv2.DataSource = dvSelected

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



为了说明的目的,这是设置为使得在DGV1中检查项目时,它们从它们消失(因为它们不再符合 Selected = False 标准),并自动显示在DGV2中(因为现在他们符合该条件。结果:





底部DGV中未选中/未选择的行将会回到顶端。



这是经济的,您不仅不需要运行任何代码来显示添加或移动到第二个DGV,而且您不会制作<$ c的副本$ c> DataRows 和一个新的 DataTable 执行 TaskManager (粗糙,但指示性),内存量大致与Selected Rows的大小相同;当您手动复制它们时,它会缓慢地爬行,因为您制作的副本包含相同的相同数据的 DataRows


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

My Goal is to show only selected rows into another DataGridView

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

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

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

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

解决方案

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.

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.

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"

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

If the data gets into the DataTable some other way, add the column manually:

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

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 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).

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:

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

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天全站免登陆