Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1155
  • Last Modified:

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
0
tcannon
Asked:
tcannon
1 Solution
 
madhevan_pillaiCommented:
try this

      If CBool(drChk.Item(0)) = CBool(chk.Value) Then
            chk.Checked = True
        End If
0
 
tcannonAuthor Commented:
I moved the call to this databind function outside of the page_Load event  If Not Page.IsPostBack Then en if

  BindData(Empid, lblFY.Text) and now it is working

I also moved the gridview binding in the BindData function.

Now it works!
0
 
Vee_ModCommented:
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now