Home > Software design >  How to verify all button in approved in gridview while click on other button that is outside the gri
How to verify all button in approved in gridview while click on other button that is outside the gri

Time:03-06

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.

CodePudding user response:

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">
            <Columns>
                <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" >
                    <ItemTemplate>
                           <asp:CheckBox ID="chkApproved" runat="server" Checked='<%# Eval("Approved") %>' 
                                CssClass="bigcheck"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="By">
                    <ItemTemplate>
                        <asp:Label ID="lblApBy" runat="server" Text='<%# Eval("ApprovedBy") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>                    
                <asp:TemplateField HeaderText="Approve">
                    <ItemTemplate>
                        <asp:Button ID="cmdApprove" runat="server" Text="Approve" CssClass="btn"
                            onclick="cmdApprove_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>


            </Columns>
        </asp:GridView>
        <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
        LoadGrid()
    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)
            conn.Open()
            Dim rstData As New DataTable
            rstData.Load(cmdSQL.ExecuteReader)
            GridView1.DataSource = rstData
            GridView1.DataBind()
        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)
            conn.Open()
            cmdSQL.Parameters.Add("@Initials", SqlDbType.NVarChar).Value = UserInitials
            cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = pkID
            cmdSQL.ExecuteNonQuery()
        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)

    Next

End Sub

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

enter image description here

  • Related