1
votes

I'm having an odd issue with the GridView. I set up a Select column, and allow sorting. If I select a row, then sort the grid, the selection stays in place and the grid sorts. That is to say, the highlighted row is changed, but the index of the selection doesn't seem to.

Looking at the grid in a watch, it seems like the selected index property actually stays the same, but the selected datakey is changing.

I'm very confused and have been unable to track down why this is happening, any thoughts?

2

2 Answers

2
votes

I found a change request for this exact issue that states the following:

It is by design that we don't keep track of selection across sorts, updates, deletes, etc. This part will not be changed.

Looks like you are going to need a different way to keep track of the selected row across sorts.

1
votes

Acctually, I found a way around this, maybe not so sweet but anyway, I got what I wanted :) In the GridView_PreRender load-event

protected void GridView1_PreRender(object sender, EventArgs e) { //trucate the text if it's to long..

        if (GridView1.Controls.Count != 0)
        {
            foreach (GridViewRow r in GridView1.Controls[0].Controls)
            {
                foreach (TableCell tc in r.Controls)
                {
                    if (tc.Text != "" && tc.Text.Length > 39)
                    {
                        tc.Text = tc.Text.Substring(0, 39) + " ...";
                    }
                }
            }
        }

        // here is the where the magic happens :)
        if (GridView1.SelectedRow != null)
        {
            GridViewRow row = GridView1.SelectedRow;
            if (row.Cells.Count > 1)
            {
                //Here I pick the p.keyID
                SetOrderData(Convert.ToInt32(row.Cells[1].Text));

                this.LabelDebug.Text = row.Cells[1].Text;
            }
        }
    }