views:

508

answers:

2

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?

+1  A: 

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.

Matthew Jones
They really can't keep track of the selected RECORD, only the selected ROW. In order to keep track of the selected record they would need to know what the primary key fields are for the dataset being represented, and this will likely be different from case to case. In one case it might be a single field called ROWID; in another case it might be a combination of three fields; in a third case the data - as it is being brought back - may not even have a primary key set.
eidylon
Ahh, well that answers that for me! At least I know I'm not insane now. I think I'll actually just drop the selected row, since I don't really care if it tracks it. I actually do keep track of selected rows in a modified multi-select grid we use. This particular single-select grid was throwing me off though. Thanks for clearing things up!
CodexArcanum
+1  A: 

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;
            }
        }
    }
Johan
What is SetOrderData?
Pieces