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?
Obvious caveat here - if you intend to sort the items in the grid then there doesn't seem to be much point in scrolling it... who knows where in the (sorted) collection the newest row will appear, but it won't (or at least shouldn't) always be at the bottom.
Simon Gillbee said: Okay... I think I might have figured it out. Does this look okay? public void AddRow(object[ rowData){ // Add new row(s) to grid UltraGridRow row = ultraGrid1.Rows.Band.AddNew(); for (int i = 0; i < rowData.GetLength(0) && i < row.Cells.Count; i++) { row.Cells[i].Value = rowData[i]; }}
Okay... I think I might have figured it out. Does this look okay?
public void AddRow(object[ rowData){ // Add new row(s) to grid UltraGridRow row = ultraGrid1.Rows.Band.AddNew(); for (int i = 0; i < rowData.GetLength(0) && i < row.Cells.Count; i++) { row.Cells[i].Value = rowData[i]; }}
Yes, that's the way to do it. :)
For what it's worth, I'm not using UltraDataSource - I'm keeping all my objects in a .NET IBindingList collection (in my case this is just a BindingList at the moment, although I may well subclass it at a later date) and binding them directly to the DataSource property on the UltraGrid. That way adding a row is as simple as calling myBindingList.Add(mynewObject) which ought to be a reasonably efficient operation (rather than looping through all existing rows!!!). The grid seems to successfully & automatically pick up whenever new objects are added to the collection (and triggers the appropriate UltraGrid eventHandlers accordingly). Presumably it should also work with any other standard (bound) data source...
I had some fun and games trying to get the binding sorted out - now that it's working I'm not going to fiddle with it! If you decide to go down a similar route, you might want to try hooking your collection (or table or whatever) up to a .Net BindingSource object, then setting the BindingSource as the grid's DataSource... play around with it and see if you can get any of them running. I had to build a separate (blank) WinForm with a single grid and a couple of buttons to trigger logging events (and nothing else) just to test exactly which binding setup actually worked on the grid, without having the clutter of heaps of other events/code to muddy the waters.
Good luck - keen to hear how you get on...
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.