[SOLVED] How to verify all button in approved in gridview while click on other button that is outside the gridview


I want to add one button in vb.net e.g bulk approval outside the gridview in another table. While clicking on that button I.e is bulk approval.. all verify button are verified in gridview in every row… and changed into verified.


Ok, so we have some kind of grid, and some kind of approve button for each row.

Ok, so we have some typical markup like this:

This is a grid (hotel bookings), and then a the "administrator" has to approve each one.

So, say some markup like this – nothing special:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table">
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                <asp:BoundField DataField="LastName" HeaderText="LastName"  />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" ItemStyle-Width="180" />
                <asp:BoundField DataField="City" HeaderText="City"  />
                <asp:BoundField DataField="Description" HeaderText="Description"  />
                <asp:TemplateField HeaderText="Approved" ItemStyle-HorizontalAlign="Center" >
                           <asp:CheckBox ID="chkApproved" runat="server" Checked='<%# Eval("Approved") %>' 
                                CssClass="bigcheck"  />
                <asp:TemplateField HeaderText="By">
                        <asp:Label ID="lblApBy" runat="server" Text='<%# Eval("ApprovedBy") %>' ></asp:Label>
                <asp:TemplateField HeaderText="Approve">
                        <asp:Button ID="cmdApprove" runat="server" Text="Approve" CssClass="btn"

        <div style="float:right">
        <asp:Button ID="cmdApproveAll" runat="server" Text="Approve All" CssClass="btn" />

And our code to load the grid:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
    End If

End Sub

Sub LoadGrid()

    Using conn As New SqlConnection(My.Settings.TEST4)

        Dim strSQL As String = "SELECT TOP 6 * from tblHotels"
        Using cmdSQL As New SqlCommand(strSQL, conn)
            Dim rstData As New DataTable
            GridView1.DataSource = rstData
        End Using
    End Using

End Sub

And we now have this:

enter image description here

Ok, so we have a plane jane button on each row. The code to approve a row is this code:

(a simple standard button in the grid view – button click)

Protected Sub cmdApprove_Click(sender As Object, e As EventArgs)

    ' approve one row
    Dim btn As Button = sender
    Dim gRow As GridViewRow = btn.NamingContainer

    Call ApproveGRow(gRow)

End Sub

Sub ApproveGRow(gRow As GridViewRow)

    ' set checkbox = checked
    Dim ckApprove As CheckBox = gRow.FindControl("chkApproved")
    ckApprove.Checked = True
    Dim lBy As Label = gRow.FindControl("lblApBy")
    lBy.Text = UserInitials

    ' Get data base pk id

    Dim pkID As Integer
    pkID = GridView1.DataKeys(gRow.RowIndex).Item("ID")

    ' Now udpate database
    Dim strSQL As String =
        "UPDATE tblHotels SET Approved = 1, ApprovedBy = @Initials WHERE ID = @ID"

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            cmdSQL.Parameters.Add("@Initials", SqlDbType.NVarChar).Value = UserInitials
            cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = pkID
        End Using
    End Using

End Sub

So, on a button click, we get the current row click, and then call our approve routine that accepts grid ro

So, if I click say on the second row, we see this:

enter image description here

However, what about do the WHOLE gird, with one button, and approve all rows?

Well, the button for the approve all looks like this:

Protected Sub cmdApproveAll_Click(sender As Object, e As EventArgs) Handles cmdApproveAll.Click

    For Each gRow In GridView1.Rows

        Call ApproveGRow(gRow)


End Sub

And if I click on it, then I get this:

enter image description here

Answered By – Albert D. Kallal

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *