I am using the UltraWinGrid as a real-time log viewer. I want the grid to automatically display each new row as it is added to the bottom. Preferably, I would like this to be a design-time setting on the grid. If that's not available, I don't mind hooking the RowInserted event and doing some processing. Just not sure what processing to do.
Hi Simon,
If you add a new row to the grid, then the grid automatically scrolls to keep that row in view. The same holds true for the ActiveRow. So if this is not working for you, then my guess is that you must be adding the rows directly to the data source and your data source must not be making the newly-added row the Current row.
Since it's not clear from your post exactly what you are adding the rows to and what you data source is, it's hard to give you a good answer. But setting the ActiveRow in the grid should do it. Or you can use methods on the grid.ActiveRowScrollRegion to scroll any row you want into view.
Thanks for the reply! I apologize for not giving enough info.
I am indeed doing all data writes via a ultradatasource and I let the data binding to the grid take care of the rest. How do you set the Current row via a data source? Or do I need to take the UltraDataRow object passed from UltraDataSource.Add() and pass it directly to UltraDataGrid.Current?
As I dig into this a bit more, I guess I'm missing some key piece of information.
(a) First I thought I would ditch the data source and just add directly to the grid. Unfortunately, I can't figure out how to add rows directly to a grid. There is not a "Add" method on UltraDataGrid or UltraDataGrid.Rows (I'm probably looking in the wrong place).
(b) I looked at ActiveRow and ActiveRowScrollRegion... they seem fairly straightforward. Except they operate on UltraGridRows... not UltraDataRows (which is what I get back from the UltraDataSource). Given a datasource row, how do I correlate it to a grid row?
I apologize for being ignorant here... I suspect that I'm missing some key architectural information about how infragistics UI does the separation of concerns between data objects and presentation objects. Unfortunately the help is not revealing in this case... it's probably buried somewhere but I'm having a great deal of difficultly mining information from either the help or from intellisense.
I had a very similar (possibly the same) problem yesterday. The way I got around it was to extend the UltraGrid class, and have the logging framework raise an event whenever the log was appended. Then the (extended) grid can have an event handler that does the appending (or just deals with the notification - your choice...) and after appending it can scroll to the end. I set it up so that the scrollbar was "sticky" - meaning it only scrolled to the end if it was already at the end before the latest addition. That way you can still browse around earlier entries, and go back to auto-scrolling by simply returning to the latest row.
Code below - it might not be a perfect setup, but it seems to be working for me so far.
Cheers,
- Sam.
public class LogViewerDataGrid : UltraGrid { . . . /// /// Handle a LogAppended event /// public void LogAppendedEventHandler(object sender, EventArgs e) { bool scrollToEnd = false; . .// setup . // if it's an empty grid, or we can already see the last row, set scrollToEnd = true if (DisplayLayout.Rows.Count > 0) { // if the last row in the grid is in the viewing area, set scrollToEnd = true UIElement rowUIElement = DisplayLayout.Rows[DisplayLayout.Rows.Count - 1].GetUIElement(); if (rowUIElement != null && rowUIElement.Rect != null) { scrollToEnd = true; } } else { scrollToEnd = true; } . .// add new row(s) to grid . // if the grid should be scrolled to the last row, do so now if (scrollToEnd) { DisplayLayout.RowScrollRegions[0].ScrollRowIntoView(DisplayLayout.Rows[DisplayLayout.Rows.Count - 1]); } } . . . }
Thanks! This is *exactly* what I was planning to do... and you saved me the time. Now if only I could figure out how to add rows directly to a grid without going through the UltraDataSource.