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?
a) The AddNew method on the grid is on the Band.
b) If you add a row to the grid, it will automatically be scrolled into view, so you don't really need to worry about it.
An UltraDataSource doesn't have a current row, this is a concept of the CurrencyManager. So you could get the CurrencyManager from the grid's BindingContext and use that.
If you want to get the grid row from an UltraDataSource row, then you would have to loop through the grid rows and examine the ListObject property of each row to find the one that matches up to the UltraDataRow - probably not the most efficient way to go.
I don't see an overload of the AddNew which accepts data... it only appears to create an empty row which the user can fill out. I guess I'm still not making myself quite clear.
I need to programatically add rows to a grid. The grid itself is entirely read-only from a user perspective. There is no ability to add, modify or delete rows from the UI (though it does support grouping, sorting, and filtering).
UltraGrid.Rows.Band.AddNew() does not take any data. And I cannot find a Add method (or anything like it) on the UltraGrid, the Rows collection or on the Band for the RowsCollection.
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]; }}
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...
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.