tcannon
asked on
How Do I Show Checkboxes Checked in Gridview Based on Data From Database?
I am trying to show checkboxes checked in a gridview, based on a datatable that I am getting from the database. I am binding the gridview with a list of suggested courses for employees to take.
Here is my gridview code:
<asp:GridView ID="gvTeamwork" runat="server" AutoGenerateColumns="False " DataKeyNames="TID"
EnableViewState="False" >
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<input id="chkTE" runat="server" value='<%#Container.DataIt em("TID")% >' type="checkbox" />
<input id="chkTE1" runat="server" visible="false" value='<%#Container.DataIt em("TID")% >' type="checkbox" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CourseName" ShowHeader="false" ReadOnly="True">
<ItemStyle Width="430px" />
</asp:BoundField>
<asp:BoundField DataField="CourseType" ShowHeader="false" ReadOnly="True">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Reference" ShowHeader="false" ReadOnly="True">
<ItemStyle Width="100px" />
</asp:BoundField>
</Columns>
</asp:GridView>
Here is my code behind:
For i As Integer = 0 To (gvTeamwork.Rows.Count - 1)
Dim chk As HtmlInputCheckBox = gvTeamwork.Rows(i).FindCon trol("chkT E")
'Check the checkboxes if employee has data
Dim dtChk As DataTable = EAP_TrainingDAC.EmpTrainin gSuggestio nGet(id, FY)
If dtChk.Rows.Count > 0 Then
' For iChk As Integer = 0 To dtChk.Rows.Count - 1
Dim drChk As DataRow = dtChk.Rows(0)
For Each drChk In dtChk.Rows
If drChk.Item(0) = chk.Value Then
chk.Checked = True
End If
Next
End If
Continue For
Next
When debugging it does go into this statement:
If drChk.Item(0) = chk.Value Then
chk.Checked = True
End If
Here is my gridview code:
<asp:GridView ID="gvTeamwork" runat="server" AutoGenerateColumns="False
EnableViewState="False" >
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<input id="chkTE" runat="server" value='<%#Container.DataIt
<input id="chkTE1" runat="server" visible="false" value='<%#Container.DataIt
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CourseName" ShowHeader="false" ReadOnly="True">
<ItemStyle Width="430px" />
</asp:BoundField>
<asp:BoundField DataField="CourseType" ShowHeader="false" ReadOnly="True">
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Reference" ShowHeader="false" ReadOnly="True">
<ItemStyle Width="100px" />
</asp:BoundField>
</Columns>
</asp:GridView>
Here is my code behind:
For i As Integer = 0 To (gvTeamwork.Rows.Count - 1)
Dim chk As HtmlInputCheckBox = gvTeamwork.Rows(i).FindCon
'Check the checkboxes if employee has data
Dim dtChk As DataTable = EAP_TrainingDAC.EmpTrainin
If dtChk.Rows.Count > 0 Then
' For iChk As Integer = 0 To dtChk.Rows.Count - 1
Dim drChk As DataRow = dtChk.Rows(0)
For Each drChk In dtChk.Rows
If drChk.Item(0) = chk.Value Then
chk.Checked = True
End If
Next
End If
Continue For
Next
When debugging it does go into this statement:
If drChk.Item(0) = chk.Value Then
chk.Checked = True
End If
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator
Vee_Mod
Community Support Moderator
If CBool(drChk.Item(0)) = CBool(chk.Value) Then
chk.Checked = True
End If