• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1994
  • Last Modified:

How to select a row inside a gridview after button click


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

1 Solution
Abu HamdanEnterprise Architect, PM ExpertCommented:
Use the gridview rowdatabound event,, write code that will compare currect bounded row with one isnerted if yes change backcolor to the highlight one
freezegravityAuthor Commented:
hmm ... well ...  how do I compare "current bounded row with inserted"
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" />

Forced accept.

EE Admin

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now