Link to home
Create AccountLog in
Avatar of tcannon
tcannonFlag for United States of America

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.DataItem("TID")%>' type="checkbox" />
                                   <input id="chkTE1" runat="server" visible="false" value='<%#Container.DataItem("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).FindControl("chkTE")

            'Check the checkboxes if employee has data
            Dim dtChk As DataTable = EAP_TrainingDAC.EmpTrainingSuggestionGet(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
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

try this

      If CBool(drChk.Item(0)) = CBool(chk.Value) Then
            chk.Checked = True
        End If
ASKER CERTIFIED SOLUTION
Avatar of tcannon
tcannon
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator