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.
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.
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...