Get checked checkbox values in a gridview

Posted on 2011-05-04
Last Modified: 2012-05-11

I have a GridView that shows id, username, add_permission, edit_permission. In this gridview add_permission, edit_permission are checkbox fields. Now if any one press submit button as shown in the image then need to get  Id, checked checkbox values

Attached code also.  My code is with VB
<asp:GridView  ID="GridView1" CssClass="nomaltext" Width="99%" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
            BackColor="White" BorderStyle="None" BorderWidth="1px" CellPadding="3" BorderColor="#999999" GridLines="Vertical">
                <asp:BoundField DataField="Id" HeaderText="Id"  InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
                <asp:TemplateField >
                <ItemTemplate >
                add permission
                <asp:CheckBox ID="chAdd" runat="server" />
                <asp:TemplateField >
                <ItemTemplate >
                Edit permission
                <asp:CheckBox ID="chEdit" runat="server" />
                <asp:TemplateField >
                <ItemTemplate >
                Delete permission
                <asp:CheckBox ID="chDelete" runat="server" />
                <asp:TemplateField >
                <ItemTemplate >
                View permission
                <asp:CheckBox ID="chView" runat="server" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="Gainsboro" />
        <asp:Button ID="Button1" runat="server" Height="45px" Text="Button" Width="74px" />

and code behind is 

        Dim dbConn As SqlConnection
        Dim DSList As DataSet
        Dim DAList As SqlDataAdapter
        Dim sqlString As String

        sqlString = "select id, username from Users"
        dbConn = New SqlConnection(ConnString)
        DAList = New SqlDataAdapter(sqlString, dbConn)

        DAList.SelectCommand.CommandTimeout = 300

        DSList = New DataSet

        If DSList.Tables.Count > 0 Then
            If DSList.Tables(0).Rows.Count > 0 Then
                GridView1.DataSource = DSList.Tables(0)

            End If
        End If


Open in new window

Question by:mshaji
    LVL 83

    Expert Comment

    LVL 2

    Accepted Solution

    you can use something like this,  after setting flag when you update your database you can use flags to determine whether or not to insert that column's value
      Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            For i As Integer = 0 To dgv.Rows.Count - 1
                If dgv.Rows(i).Cells(2).Value = True Then 'Checked
                      set flag
                Elseif dgv.Rows(i).Cells(3).Value = True Then 'Checked
                     set flag
                Elseif dgv.Rows(i).Cells(3).Value = True Then 'Checked
                     set flag
                Elseif dgv.Rows(i).Cells(3).Value = True Then 'Checked
                     set flag
                End IF
            End If
        End Sub

    Open in new window


    Author Comment


    Author Closing Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now