Not logged in - Login

Keep sort after row added to grid

The following code demonstrates how to keep the grid sorted and highlight the newly added row on an Infragistics grid control.

// get a reference to the grid
var grd = this.myGrid;

// get the count of the rows in the grid
int lGridRowCount = grd.Rows.Count;
if (lGridRowCount <= 0) break;   // no line? should never be. but, if so, get out now.

// activate the last row. (the one that was just added)
grd.Rows[lGridRowCount - 1].Activate();

// get the active row (the new one that was just added)
var activeRow = grd.ActiveRow;

// get the length
int length = grd.Rows.All.Length;

// get it to sort.
grd.Rows[length - 1].RefreshSortPosition();

// at this point, the grid has been resorted, but the correct row is may not be visible.

if (lGridRowCount > 0 && activeRow != null)
{
   // first, make the top row visible.
   grd.ActiveRowScrollRegion.ScrollRowIntoView(grd.Rows[0]);

   // make sure the active row is visible.
   grd.ActiveRowScrollRegion.ScrollRowIntoView(activeRow);
}