Click here to Skip to main content
15,565,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having issue when I select a row after sorting it switches back to row index 0. I would like to able to sort or search and select spesific row. So the data can be sent to my text boxes for updating. Here are my codes.

protected void GridViewSys_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridViewSys, "Select$" + e.Row.RowIndex);
                e.Row.ToolTip = "Click to select this row.";

protected void GridViewSys_SelectedIndexChanged(object sender, EventArgs e)
            foreach (GridViewRow row in GridViewSys.Rows)
                if (row.RowIndex == GridViewSys.SelectedIndex)
                    row.BackColor = ColorTranslator.FromHtml("#007bff");
                    row.ToolTip = String.Empty;
                    String sysName, sysDesc, sysVen, sysReq, sysPO, sysLT, sysSOW, sysSLA, sysComp, sysSQ;
                    sysName = row.Cells[0].Text;
                    sysDesc = row.Cells[1].Text;
                    sysVen = row.Cells[2].Text;
                    sysReq = row.Cells[3].Text;
                    sysPO = row.Cells[4].Text;
                    sysLT = row.Cells[5].Text;
                    sysSOW = row.Cells[6].Text;
                    sysSLA = row.Cells[7].Text;
                    sysComp = row.Cells[8].Text;
                    sysSQ = row.Cells[9].Text;

                    txtSysName.Text = sysName;
                    txtSysDesc.Text = Server.HtmlEncode(sysDesc);
                    txtSysVen.Text = sysVen;
                    txtSysReq.Text = sysReq;
                    txtSysPO.Text = sysPO;
                    txtSysLT.Text = sysLT;
                    lblSow.Text = sysSOW;
                    lblSLA.Text = sysSLA;
                    lblComp.Text = sysComp;
                    lblSQ.Text = sysSQ;
                    row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
                    row.ToolTip = "Click to select this row.";

What I have tried:

Looked all over the internet but no luck. Honestly I have no clue how to fix this.
Updated 10-Jul-22 5:48am
Richard MacCutchan 10-Jul-22 3:12am    
It is not clear what you mean by " after sorting it switches back to row index 0". The RowIndex is a dynamic value and reflects the item's current position in the grid. So the value before sorting may well be different after sorting.
Kong Lee 2022 10-Jul-22 9:07am    
for example, if I sort Z - A I want to be able to select any row and still retain row selected on the gridview. I tried using:
protected void GridViewSys_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
GridViewRow row = GridViewSys.Rows[e.NewSelectedIndex];
but does not work. Also tried using:
GridViewSys.SelectedIndex = e.NewSelectedIndex;

Share this answer
Kong Lee 2022 10-Jul-22 2:57am    
Yes I've tried it and it does nothing for me.
OriginalGriff 10-Jul-22 3:39am    
And what - exactly - did you try?
What effects did it have?
What did you expect it to do?
Ah, fixed problem. I just rebuilt the project and everything seems to work like it supposed too. No clue what happened.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900