Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = scon
.Open()
End With
Dim sql1 As String = ""
sql1 = "SELECT Student.St_Id, Title.TT_Name, Register.Rg_Name, Register.Rg_SurName, Student.St_pass1 FROM Student INNER JOIN Register ON Student.Rg_Idcard = Register.Rg_Idcard INNER JOIN Title ON Register.TT_ID = Title.TT_ID"
sql1 &= " ORDER BY Student.St_Id"
Dim ds As New DataSet
da = New SqlDataAdapter(sql1, conn)
da.Fill(ds, "Student")
If ds.Tables("Student").Rows.Count <> 0 Then
Dim dt As DataTable
dt = ds.Tables("Student")
For Each dr As DataRow In dt.Rows
chx = dr("St_pass1")
If chx = "1" Then
CType(gvResult1.FindControl("CheckBox1"), CheckBox).Checked = True '<--- มีปัญหาช่วงนร้อะครับ
End If
Next
End If
gvResult1.DataSource = ds.Tables("Student")
gvResult1.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = scon
.Open()
End With
Dim sql1 As String = ""
sql1 = "SELECT Student.St_Id, Title.TT_Name, Register.Rg_Name, Register.Rg_SurName, Student.St_pass1 FROM Student INNER JOIN Register ON Student.Rg_Idcard = Register.Rg_Idcard INNER JOIN Title ON Register.TT_ID = Title.TT_ID"
sql1 &= " ORDER BY Student.St_Id" Dim ds As New DataSet '<-- เอาไปประกาศเป็น Global
ds = New DataSet() '<-- เพิ่มโค้ด
da = New SqlDataAdapter(sql1, conn)
da.Fill(ds, "Student") If ds.Tables("Student").Rows.Count <> 0 Then Dim dt As DataTable dt = ds.Tables("Student") For Each dr As DataRow In dt.Rows chx = dr("St_pass1") If chx = "1" Then CType(gvResult1.FindControl("CheckBox1"), CheckBox).Checked = True '<--- มีปัญหาช่วงนร้อะครับ End If Next End If
gvResult1.DataSource = ds.Tables("Student")
gvResult1.DataBind()
End Sub
เพิ่มอันนี้
Code (VB.NET)
Dim ds As DataSet
Protected Sub gvResult1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResult1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim Cb As CheckBox = CType(e.Row.Cells(index ของ checkboxfield).Controls(0), CheckBox)
If ds.Tables("Student").Rows(e.Row.RowIndex)("St_pass1").ToString() = "1" Then
Cb.Checked = True;
End If
End If
End Sub