簡體   English   中英

如何使用Windows窗體VB.net將復選框列添加到網格視圖

[英]How to add checkbox column into grid view using Windows form VB.net

Public Class Recipients
Private Sub Recipients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    mycom.Connection = cn
    mycom.CommandText = "Select Idno,Name,Course,YearSec,Organization from tbl_students"
    myr = mycom.ExecuteReader

    While myr.Read
        With grdRecipients
            .Rows.Add()
            .Rows(.RowCount - 1).Cells(0).Value = myr(0).ToString
            .Rows(.RowCount - 1).Cells(1).Value = myr(1).ToString
            .Rows(.RowCount - 1).Cells(2).Value = myr(2).ToString
            .Rows(.RowCount - 1).Cells(3).Value = myr(3).ToString
            .Rows(.RowCount - 1).Cells(4).Value = myr(4).ToString
        End With
    End While
    myr.Close()
End Sub
End Class

我有一個名為grdRecipients的網格視圖,該視圖使用select語句將數據加載到數據庫中。 列名是在數據網格視圖中手動輸入的。 自動創建自己的列名並添加復選框列的任何幫助。

希望得到任何幫助。 謝謝

若要自動創建列名,可以基於SQL查詢創建一個DataTable並將其設置為DataGridView的源。 要具有CheckBox列,DataTable中必須有一列具有布爾數據類型。 可以手動創建它,也可以從查詢中檢索它。

檢查以下代碼以獲取更多信息

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mycom.Connection = cn
    mycom.CommandText =
        <SQL>
            SELECT
                Idno
                ,Name
                ,Course
                ,YearSec
                ,Organization
            FROM tbl_students
        </SQL>.Value

    If cn.State = ConnectionState.Closed Then
        cn.Open()
    End If

    Dim myadap As New SqlDataAdapter(mycom)
    Dim mydt As New DataTable
    myadap.Fill(mydt)

    mydt.Columns.Add("CheckBoxColumn", GetType(Boolean))
    grdRecipients.DataSource = mydt

    myadap.Dispose()
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM