Andris Mudiayi
Andris Mudiayi

Reputation: 469

How to select a row without using OnSelectedIndexchanged in asp.net

In an ASP.NET GridView control

Upvotes: 0

Views: 1828

Answers (3)

Rahul
Rahul

Reputation: 5636

Yes it is possible to select a row without using the OnSelectedIndexChanged event.

You have to use RowCommand event of GridView like

C#

    protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ProcessThis")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = Gridview1.Rows[index];
                // Do what ever you want....
            }
    }

.aspx

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" 
                        onrowcommand="Gridview1_RowCommand">
                        <Columns>
                        <asp:ButtonField Text="Process This" CommandName="ProcessThis" />
                        </Columns>
                    </asp:GridView>

You have to assign CommandName in the button that you are using inside your GridView.

Hope you understand and works for you.

Upvotes: 1

to StackOverflow
to StackOverflow

Reputation: 124746

You could simply add a custom button to the GridView whose CommandName property is Select. Then use the SelectedIndexChanged event as usual.

Why do you say you don't want the SelectedIndexChanged event to fire? Note that this event will fire even if you change the selected index in code by calling the GridView.SelectRow method.

Upvotes: 1

Patrick
Patrick

Reputation: 17973

You can use the RowCommand event on the gridview for that purpose. It is triggered every time a button is clicked in the gridview.

If you use the DataKeys property in your markup you can fetch a specific ID for instance from the row of the button, in order to process a specific row. Examples are available in the docs.

Upvotes: 0

Related Questions