Ultrawingrid - Select row based on unique value

Mike Baxter picture Mike Baxter · Mar 6, 2013 · Viewed 16.1k times · Source

Is there a way to select a row in an ultrawingrid based on the value of one of its columns (ID column)? I have been trying to find out how to do this with little success.

I have a global variable that is the 'active ID' (i.e the ID that is currently being edited within the application - it is the ID that the system sees as being selected and active) - but sometimes the selected row of the grid and the 'selected ID' variable don't match. I need to make sure they are the same in order to prevent user confusion. I am hoping to call the following code inside a refresh() function...

Perhaps something like (kinda-pseudo-code-ish):

int index; // This could be any number

foreach (row r in grid)
{
    if (row.cell["ID"].value = index)
        grid.selectedindex = thisRow;
}

Am I thinking along the right lines? If so, what is the correct syntax? If not, how else should I do this?

Answer

Mike Baxter picture Mike Baxter · Mar 6, 2013

Got it.

            int index;
            foreach (UltraGridRow row in grid.Rows)
            {
                if (Convert.ToInt32(row.Cells["ID"].Value) == index)
                {
                    grid.ActiveRow = row;
                    break;
                }
            }

Works just how I needed it to - sorry for answering my own question ;)