Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vb.net - 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 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...