質問

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?

役に立ちましたか?

解決

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 ;)

他のヒント

Yes. You can use the FirstOrDefault function to find a row matching a criteria:

var row = ultraGrid1.Rows.FirstOrDefault(r => r.Cells["Id"].Value.ToString() == "1");

Now that you (potentially) have the row where the cell contains the value you'd like, you can activate it to select it:

if (row != null)
row.Activate();

If you are bound to a DataTable or a list that has the ability to find an item by key, you can use the GetRowWithListIndex method of the Rows collection to find the UltraGridRow.

For example the following will activate the row with a key of 5:

DataTable dt = this.ultraGrid1.DataSource as DataTable;
DataRow dr = dt.Rows.Find(5);
this.ultraGrid1.Rows.GetRowWithListIndex(dt.Rows.IndexOf(dr)).Activate();

If your list doesn't support finding an item by key, you could use linq to find the item in the list as well. There is an example of finding an item with link here.

If you have multiple bands you can use the following:

int index;

ultraGrid1.DisplayLayout.Bands.OfType<Infragistics.Win.UltraWinGrid.UltraGridBand>()
    .SelectMany(s => s.GetRowEnumerator(Infragistics.Win.UltraWinGrid.GridRowType.DataRow)
    .OfType<Infragistics.Win.UltraWinGrid.UltraGridRow>())
    .Where(s => s.Cells.Exists("ID"))
    .FirstOrDefault(s => (int)s.Cells["ID"].Value == index)?
    .Activate();

Note: Null-conditional Operator (?) requires C# 6.0 or higher. Otherwise you have to check, if FirstOrDefault(...)!=null and activate it then.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top