I am listing a collection of date ranges (each date range is represented by a Period object) in a DevExpress XtraGrid (WinForms).
In a method which splits a Period in two (the split date is entered by the user), the code does this :
- Obtains a Period reference on the focused row. ((Period)GridViewPeriods.GetRow(selectedRows[0]);)
- Clones the selected period.
- Obtains a reference on the newly cloned Period.
After the job is done, I want to put the focus on the newly cloned Period. At this point, I only have my two Period references.
I haven’t found any method in the XtraGrid that could locate a row based on it’s value (like returning a RowHandle by passing an object).
I ended up writing this helper method :
public int GetDataRowIndex(RatePeriod period)
{
int foundIndex = 0;
for (int i = 0; i < GridViewRatePeriods.DataRowCount; i++)
{
if ((RatePeriod)GridViewRatePeriods.GetRow(i) == period)
{
foundIndex = i;
break;
}
}
return foundIndex;
}
Then I set the focus on the newly created Period like this :
GridViewPeriods.FocusedRowHandle = GetDataRowIndex(tailingPeriod);
Is there any shorter or smarter way to do that ?
It looks like you are doing it the best way though it seems like you are having to go through a bunch of stuff to get what you are looking for Dev Express has the same solution as you have
http://www.devexpress.com/Support/Center/p/A1488.aspx
I thought maybe the rowindex could be related back to the order of your datasource but there does not appear to any collation
http://www.devexpress.com/Support/Center/p/A1488.aspx
If you are displaying any kinda unique identifying infomation in a single cell you might be able to use LocateByValue if you were not displaying the unique identifying infomation you could always hide that column