How to select a row inside a gridview after button click

Posted on 2007-10-16
Last Modified: 2012-05-05

Hopefully this is not too hard.

I have a form that inserts into a table on SQL Server some values. Below the form, I have a gridview that is based on that table. After I insert, I want the gridview to highly the row that was inserted.

So, the form has these textbox fields (corresponding to the names in the table):
FirstName, LastName, Email

I want the gridview to select/highlight the rows of the query:
SELECT * FROM tblUser WHERE FirstName = InsertedFirstName AND LastName = InsertedLastName AND Email = InsertedEmail

Question by:freezegravity
    LVL 7

    Expert Comment

    by:Abu Hamdan
    Use the gridview rowdatabound event,, write code that will compare currect bounded row with one isnerted if yes change backcolor to the highlight one

    Author Comment

    hmm ... well ...  how do I compare "current bounded row with inserted"
    LVL 7

    Accepted Solution

    here is what you do:

    protected void editGrid_RowDataBound(object sender, GridViewRowEventArgs e)
            //generic row data bound handler to add the DataItemIndex to the edit button's CommandArgument
            if (e.Row.RowType == DataControlRowType.DataRow)
                //string editIndex = e.Row.DataItemIndex.ToString();
                int _info_id = (int)((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[0];
                int _id = (int)((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[1];
                string editIndex = _info_id + ";" + _id;
                ImageButton btn = e.Row.FindControl("btnEdit") as ImageButton;
                if (btn != null) { btn.CommandArgument = editIndex; }
        protected void btnEditPerson_Click(object sender, EventArgs e)
            ImageButton btnEdit = sender as ImageButton;
            GridViewRow row = (GridViewRow)btnEdit.NamingContainer;
            string ca = btnEdit.CommandArgument;
            //i have to loop through here to seprate ids from ";"    }

    <asp:ImageButton ID="btnEdit" runat="server" ImageUrl="Images/details_icon.gif" OnClick="btnEditPerson_Click" CausesValidation="false" />

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    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