[SOLVED] Stuck trying to run a rowcommand across the whole gridview


I need to run a rowcommand across all the elements of a gridview to save clerks some time but I’m stuck with an action. This is the button that triggers the action:

Protected Sub lbMasivo_Click(sender As Object, e As EventArgs) Handles lbMasivo.Click
        Dim args = New CommandEventArgs("GestionarCampoAdicional", "")
        For Each item As GridViewRow In gv1.Rows
            Dim arg = New GridViewCommandEventArgs(item, args)
            gv1_RowCommand(item, arg)
        ShowMessage(TipoMensaje.MsgError, "Done")
End Sub

Then in the original command there’s this function that throws an exception (on gv1_rowcommand):

Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)

However, item is supposed to be a GridViewRow isn’t it? As you might guess I’m not knowledgeable in vb or asp but I’m trying to learn.


then you need to move the code out to a seperate routine for that row command. In other words, we ASSUME that you have a button OUTSIDE of the grid, say "check" all or whatever.

So, what is the code in the row command supposed to do? Without that information we are all VERY lost.

So, say I have gv, and I want a "check all" box?

Say like this:

<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" cssclass="table">
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName"   />
        <asp:BoundField DataField="City" HeaderText="City"           />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
        <asp:BoundField DataField="Description" HeaderText="Description"  />

        <asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
                <asp:CheckBox ID="chkActive" runat="server"
                    Checked='<%# Eval("Active") %>'/>
        <asp:TemplateField HeaderText="Rating">
                <asp:DropDownList ID="cboRating" runat="server"
                    DataTextField="Rating" >
<div style="float:right">
    <asp:Button ID="cmdCheckAll" runat="server" Text="Make All Active" CssClass="btn" />

Code to load the gv would be:

Dim rstCboChoice As DataTable
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()

    rstCboChoice = MyRst("SELECT ID, Rating FROM tblRating ORDER BY ID")
    ' Now load our grid
    GHotels.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")

End Sub

Protected Sub GHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GHotels.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim cboRating As DropDownList = e.Row.FindControl("cboRating")
        cboRating.DataSource = rstCboChoice
        cboRating.Items.Insert(0, "Not Selected")
    End If

End Sub

And now we have this:

enter image description here

Now, we can check box each row. Or we can choose the button outside of the grid, and process EACH row, say like this:

Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click

    For Each gRow As GridViewRow In GHotels.Rows

        Dim ckBox As CheckBox = gRow.FindControl("chkActive")
        ckBox.Checked = True


End Sub

So, whatever code you have or WANT to run and occur for each row, then you put it inside of the above loop. So, you not shared with us what kind of processing or code you need or want to run for each row, but whatever the heck that code is? You put it in the above loop.

Answered By – Albert D. Kallal

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

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