ดิฉันทำอะไรผิดไปหรือเปล่าหรือมีวิธีอื่นที่ดีกว่านี้มั้ยช่วย
นี่เป็น Sub ที่ใช้ในการกำหนดการ select ค่ะ
Code (VB.NET)
Sub InsertDepart2()
Dim i As Integer
For i = 0 To cblSumDepart.Items.Count - 1
If cblSumDepart.Items(i).Value = "Y" Then
cblSumDepart.Items(i).Selected = True
End If
Next
End Sub
Imports System.Data
Partial Class VbCheckBoxList
Inherits System.Web.UI.Page
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
CheckBoxList1.DataSource = GetDataSource()
CheckBoxList1.DataTextField = "Text"
CheckBoxList1.DataValueField = "Value"
CheckBoxList1.DataBind()
End Sub
Protected Sub CheckBoxList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.DataBound
For Each item As ListItem In CheckBoxList1.Items
item.Selected = If((item.Value = "Y"), True, False)
Next
End Sub
Protected Function GetDataSource() As DataTable
Dim Dt As New DataTable()
Dt.Columns.Add(New DataColumn("Text", System.Type.GetType("System.String")))
Dt.Columns.Add(New DataColumn("Value", System.Type.GetType("System.String")))
For i As Integer = 1 To 5
Dim Dr As DataRow = Dt.NewRow()
Dr("Text") = String.Format("tungman{0}", i.ToString())
Dr("Value") = If((i Mod 2 = 0), "Y", "N")
Dt.Rows.Add(Dr)
Next
Return Dt
End Function
End Class