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语句加载数据库中的数据。在数据网格视图中手动输入列名称。 有关自动创建自己的列名和添加复选框列的任何帮助。
感谢任何帮助。 。 。感谢
答案 0 :(得分:1)
要自动创建列名,可以根据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