Is there any way to set the datasource of a ultragrid without removing its summarize layout?
I have a ultragrid(gvResult) in my winform application.
User can select the columns and also set the sort of them in another form and then apply these changes to gvResult by pressing Apply button.
In addition gvResult must show a row counter summary.
I had to clear gvResult, like below, before applying user's changes to it, otherwise the sort of its columns had not changed to what the user had set.
gvResult.DataSource = new DataTable();gvResult.DataSource = dataTable_With_New_Set_And_Sort_of_Columns;
but this code have another problem! It removes the row counter summery too, with the other layout settings of gvResult.I searched on infragistics forum and i fined the following code, but it has the first problem, I mean the sort of the columns will remain without any changes.
BindingSource bs = new BindingSource(); bs.DataSource = typeof(DataTable); bs.DataSource = dataTable_With_New_Set_And_Sort_of_Columns; gvResult.DataSource = bs;
Do you have any suggestions?
Excuse me because of my poor english language.
I'm waiting for any suggestions......
Why no one answered?! Please help me..
Hi,
If you set the DataSource property on the grid, the grid throws away the current DisplayLayout in favor of a new layout for the new data source.
So there are a number of ways you can handle this:
1) Don't set the DataSource, but instead, change the data in the existing data source to the new data you want.
2) Save the DisplayLayout before you set the DataSource and then restore it afterward. You would do this using the grid.DisplayLayout.Save and grid.DisplayLayout.Load methods.
3) Set up your summary and other layout-related setting in the InitializeLayout event of the grid, which will fire whenever the grid's DataSource is set.
Hi mr. Saltzman, and thanks for your reply.
1) I think your first solution is not the best way for me, because the number of my columns are about 200, and if user had chosen just for example one of them to see(so i should hide 199 columns) or if he had changed the order of many of them ,maybe it takes a lot of time to change the data in the existing data source, am i right?
2) I tried this solution but it has an error when i passed a stream as parameter of Save() and Load() methods, and it did not save summery layout when i passed a filename(of a txt file) to them.
System.IO.Stream buffer = System.IO.Stream.Null;
gvResult.DisplayLayout.Save(buffer);
gvResult.DataSource = null;
null;
gvResult.DataSource = dataTable_With_New_Set_And_Sort_of_Columns;
dataTable_With_New_Set_And_Sort_of_Columns;
gvResult.DisplayLayout.Load(buffer);
< Error Message : Attempting to deserialize an empty stream. >
I think this way can be my solution but i do not know what should i pass as the parameter ?
3) I have tried this way already, but it has a problem too.You can not set the SourceColumn of your summery to a hidden column in InitializeLayout and if you do this the summary will become hidden.
The only reason I can think of why #2 would not work is if your new data structure doesn't match the old one. I was under the impression that your data was changing, but the structure is not. For example, you are not adding or removing columns or bands from the data source. The new data source has the same columns and bands as the original data source and they are all the same data types. But maybe I was mistaken.
If your data source has different columns, then you can't method #2. The best way to handle this would be to use the InitializeLayout event of the grid in that case.
Hi mr. Saltzman and thanks for your complete reply and excuse me because of my delay,
2) I tried it and it caused the error to not appear again but my summary did not display too.
3) OHHH, I did not pay attention to these overloads of the Add method that contains SummaryPosition,It is great!
However i think your 2nd solution still can be the best one for this problem, because it seems to save all of layout settings during datasource changes and it is too usefull if you wants to set different types of setting in layout of your grid.Can you please give me a sample of it?
1) Okay.
2) When you save to a stream, the stream ends up positioned at the end. So you have to set the stream.Position to 0 before you load.
3) This depends on your settings and which overload of the Summaries.Add method you are calling. It sounds like you are calling an overload that only takes in a Source column and uses the same column to position the summary. But you could easily call another overload which takes in a position column to which the summary will be aligned, or a SummaryPosition which positions the summary to the left, right, or center of the band.