genespos genespos - 6 months ago 28
Vb.net Question

Show checked rows into another DataGridView

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
DataGridView
and 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?

Answer

I propose that you add column "Sel" as Boolean in your datatable "DT_Events" . Change your first code to :

 DT_Events.Columns.Add("Sel", GetType(Boolean))
  DT_Events.Columns("Sel").SetOrdinal(0)
With Me.DataGridView1
    .Columns.Clear()
    .DataSource = DT_Events
    .Columns("Event").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
End With

And the seconde code will work with copying the selected row into "DataGridView2"

Comments