How Do I Show Checkboxes Checked in Gridview Based on Data From Database?

Posted on 2007-10-04
Last Modified: 2013-11-07
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" >
                            <asp:TemplateField ShowHeader="false">
                                   <input id="chkTE" runat="server" value='<%#Container.DataItem("TID")%>' type="checkbox" />
                                   <input id="chkTE1" runat="server" visible="false" value='<%#Container.DataItem("TID")%>' type="checkbox" />
                          <asp:BoundField DataField="CourseName" ShowHeader="false" ReadOnly="True">
                                <ItemStyle Width="430px" />
                                <asp:BoundField DataField="CourseType" ShowHeader="false" ReadOnly="True">
                                <ItemStyle Width="50px" />
                                <asp:BoundField DataField="Reference" ShowHeader="false" ReadOnly="True">
                                <ItemStyle Width="100px" />

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
            End If
            Continue For

When debugging it does go into this statement:

 If drChk.Item(0) = chk.Value Then
         chk.Checked = True
 End If
Question by:tcannon
    LVL 12

    Expert Comment

    try this

          If CBool(drChk.Item(0)) = CBool(chk.Value) Then
                chk.Checked = True
            End If

    Accepted Solution

    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!
    LVL 1

    Expert Comment

    Closed, 250 points refunded.
    Community Support Moderator

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now