Hi
I use Ultragrids, attach a data source and then customise the columns in the grid's designer. But if there are hundreds of columns, it's very slow and cumbersome to change each column one by one.
While I can hide and exclude from the field chooser a subset of columns, there are some settings we like to apply to all columns of a specific datatype. For example we like numeric columns to be right aligned, not left. We like the FilterOperandDropDownItems to be Custom for numeric columns and Customs & Blanks for String columns. Now I've managed to do this at run-time but it can take over a second to do this when the grid has 300 columns.
I had a look at presets but they don't seem to allow customisation by column Datatype.
Is there a more efficient way to do this either at design-time or run-time?
Thanks
John
Hi John,
I really wouldn't recommend using the designer when dealing with more than 10 columns or so. It can be done, and the end result may be slightly more efficient, but even dealing with 300 or 5000 columns looping through them shouldn't be any more noticeable time to process. I am not sure how you are currently doing it, but the way I would handle a scenario like that would be to use the InitializeLayout event. This event fires when you set the DataSource property of the grid. In it you have the e.Layout event arg, in which equates to the DisplayLayout of the grid. So for a single band grid with 300 columns I would just loop through the columns there, via e.Layout.Bands[0].Columns, and then just handle what you need to there. foreach(UltraGridColumn column in e.Layout.Bands[0].Columns){ if (column.DataType == typeof(string)) ...
}The only way I could imagine you might get a bit more speed out of a looping scenario, or more specifically you might slow down a looping scenario is if you created your appearance objects inside the loop. If you have a set number of appearance changes that you want applied. Instead of setting them all inside the loop which then would instantiate an appearance object for each. Define them outside of the loop and set the appropriate appearance property to the instance. Hope this helps,
Hi Mike
It was your second suggestion that identified the problem. I was setting appearances, filter row operands, etc. within the loop. Moving it to before the loop means it's dropped from 1.3 to 0.20 seconds.
I did originally use the InitializeLayout event but it meant I couldn't customise columns afterwards because the InitializeLayout code would always override them.
Thanks for your help
Glad that worked out for you. InitializeLayout fires when you set the Datasource property, and that should be the only time it fires. If you change your data source by setting the DataSource property again, then it will fire again. But with that in mind, as long as your subsequent customizations are done after that then those customizations should always take precedence. Now if you set your customizations before setting the datasource, then when you do end up setting your datasource, then yes IntializeLayout will wipe out any settings you currently have. But in the end it just depends on the logic flow of your application and what works for you, for many scenarios InitializeLayout works for developers, if your logic flow seems better doing it outside the event, then that works too. :)
Happy Coding,