The DataTable my Grid is bound has DataColumns added to it at run time.
I need this newly added DataColumn positioned correctly between two existing columns on the Grid.
What is the best way to make this happen?
1. Change the new DataColumn's ordinal position on the DataTable to be placed between the two column? Would doing so make the new UltraGridColumn show correctly between the corresponding two UltraGridColumns?
2. Not change the DataColumn ordinal. But instead position the new UltraGridColumn directly.
Questions
- Ideally it is #1 because then all my positioning logic can reside in the "data layer" logic. But I am not sure it will do the job for positioning corectly on Grid. Comment?
- If I have go with #2, how does the Grid get notified of a new UltraGridColumn being added? Any sample code (KB entry, etc) appreciated.
Thanks!
vrn said:The trouble is that VisiblePosition does not change at all and is stuck at value 13. I even tried hardcoding a value 30 but it does not change.
There is no reason I can think of why the property would not change. What exactly do you mean when you say it's not changing? Are you setting it and then checking the value in code immediately? Or are you checking it at some later point?
My guess is that you are loading a layout into the grid or something like that. And this is re-setting the VisiblePositions back to what they were.
If you are using UltraDataSource, I figured out that grid can get notification about data column being added via ListChanged event of datasource:
( (UltraDataSource) grid.DataSource).Rows.ListChanged += OnDataSourceRowsListChanged;
private void OnDataSourceRowsListChanged(object sender, ListChangedEventArgs e) {
// in case of adding column ListChangedType equals to PropertyDescriptorAdded.
if (e.ListChangedType == ListChangedType.PropertyDescriptorAdded) { // make your actions on grid. } }
If you are using System.Data.DataTable you can subscribe on CollectionChanged event which occurs when the columns collection changes, either by adding or removing a column.
if (grid.DataSource is DataTable){
((DataTable)grid.DataSource).Columns.CollectionChanged += delegate(object sender, CollectionChangeEventArgs args)
{
// make your actions on grid.
};
}
I tested this and it works, but I could wrong in nuances.
Thanks, Alex.
Mike, I took you advice and I am setting the VisiblePosition of the column
I have this line of code which says
totalColumn.Header.VisiblePosition = (this.DisplayLayout.Bands[i].Columns[dateBucket.RelativeColumn].Header.VisiblePosition + 1);
There are number of totalColumns I set the VisiblePosition for. So I iterate and I set them relative to the poistion of some RelativeColumn
The trouble is that VisiblePosition does not change at all and is stuck at value 13. I even tried hardcoding a value 30 but it does not change.
I do not have RowLayout unless
Hi,
There's no event on the grid for this. It seems like you need to respond to an event triggered by the data layer and then ensure that the grid is refreshed before you try to access columns on the grid.
What exactly are you attempting to acheive here? Perhaps there is some other way to acheive what you want, rather than watching for the creation of a new column in the grid.
Hi MIke,
I am not sure you had a chance to read my question. So awaiting to hear from you on which event to use.
In response to my original email, you said
Mike Saltzman"] This is the way to go. The grid gets gets a notification from IList or IBindingList implementation of the data source. You could hook the same events. But since your code is adding the new column, you really don't need an event to tell you that this happen. You can simply add the column to the data source, then look for a column in the grid with the same key. You might have to call Refresh on the grid first to force the grid to paint, because the grid column might be created lazily.
This is the way to go. The grid gets gets a notification from IList or IBindingList implementation of the data source. You could hook the same events. But since your code is adding the new column, you really don't need an event to tell you that this happen. You can simply add the column to the data source, then look for a column in the grid with the same key.
You might have to call Refresh on the grid first to force the grid to paint, because the grid column might be created lazily.
My problem is that the code adding the column is in the "data layer" of the application which does not know or care about the Grid. While the Grid itself is know the presentation layer. So I would like the presentation code to find out that a column got added and then position it. What event should my grid be handling for my requirement?