This is a migrated thread and some comments may be shown as answers.

Accessing the column collection for AutoGenerated columns

9 Answers 413 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyle
Top achievements
Rank 1
Kyle asked on 04 Sep 2008, 03:26 PM
We are currently migrating our current toolset to the telerik RadControls toolset.  Out current code relys on being able to access the column collection to apply user settings and other features.  Is this possible to do with auto generated columns without manually adding the columns by hand?  It seems that the column collection is always empty when using auto generate.  I have read the help on using the ColumnCreated event, however this would require changing a lot of code to be compliant. 

Thank you.

9 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 04 Sep 2008, 03:34 PM
Hello Kyle,

Please try the RadGrid1.MasterTableView.RenderColumns collection.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Kyle
Top achievements
Rank 1
answered on 04 Sep 2008, 03:38 PM
Daniel,

I have noticed this construct.  However, the help documentation states that you should not use this construct to make changes or customizations to the columns.  Is this an acceptable practice?

Thanks,
Kyle
0
Daniel
Telerik team
answered on 04 Sep 2008, 03:50 PM
Hello Kyle,

The reason is that it's too late to make a changes to the column collection at this point. Could you please elaborate a bit on your scenario in order to give you a straight-to-the-point answer?

Greetings,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Kyle
Top achievements
Rank 1
answered on 04 Sep 2008, 04:03 PM
Daniel,

Firstly, let me thank you for responding so quickly.  I will try to elaborate on our process to give you a better idea of what we are trying to accomplish.

First we bind our grid to a datasource such as a datatable.  This table is dynamically built via the supplied sql statement but will contain all of the columns needed for processing information such as edit, selection and keys.  The data is bound and then in the preRender function we would like to iterate through column collection to set column headers, showing and hiding columns based on user preference, column widths, custom column editors, and column maps (ie. images, key mapping, etc.)  The problem is that with auto generated columns there seems to be no way to access a collection of columns to make these adjustments to the columns.  The only event I have seen which gives us a column is the ColumnCreated event, but as I stated earlier, this will require reworking lots of code.  When does the column collection actually become accessible when using the auto generated columns?  Also using the RenderColumns array as you suggested may be an option, if that is even possible.  However, this does not provide methods such as FindByUniqueName which we would prefer to use.  I hope that gives you enough information to understand the situation.

Thanks,
Kyle
0
Accepted
Daniel
Telerik team
answered on 08 Sep 2008, 09:49 AM
Hello Kyle,

Actually as I said before it may be too late for some operations to execute on RadGrid PreRender event. For example:

- to rename column header - you need to rebind RadGrid
- to change header item size - ok
- to change visibility to hidden - ok

This is a possible approach to get a column index for RenderColumns:
GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ProductName"); 
if(column != null
    RadGrid1.MasterTableView.RenderColumns[column.OrderIndex].Visible = false

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ed
Top achievements
Rank 1
answered on 09 Sep 2008, 02:37 PM
I have a related issue.  With radGrid for ASP.Net, there were many things that could be done in PreRender.  One in particular that worked quite nicely was reordering of columns as well - and to some extent that still works.

I find that hiding columns does seem to still work (col.Visible is still obeyed), as does setting column header styles (for instance to chance style on column labels of columns involoved in any filtering of the data). Setting widths seems to somewhat work, but I am seeing some idiosynchrasies I still need to finish debugging.

Unfortunately, I am finding that column ordering one key difference now with RadGrid for ASP.Net AJAX.  While I can reorder columns in PreRender by explicitly looping through and setting the OrderIndex (works just fine just as it did for RadGrid for ASP.Net), I can no longer get back to the order in the underlying DataTable by just rebinding, which I could before. 

To clarify, I have 3 buttons related to grid state for the user to use.  One saves the current column order, widths, filter, and sort settings for the current user.  Works fine.  One restores the grid to the last saved settings of the user, so if they have since changed the sort, or dragged columns around, the get back to how they last saved.  The third is meant to restore the grid to it's order, sort, and filter state as if they never have manipulated it at all and had no user specific settings.  With radGrid for ASP.Net, all that required was to rebind the grid and let it process without manipulation.  But with RadGrid for ASP.Net AJAX, the grid (specifically the RenderColumns collection) seems to 'remember' things like column order across rebinds.  I haven't found a way (yet) to clear out the prior state to return to the way the autobound data would have displayed originally.  At first, I thought maybe the client side was now retaining the manipulated order, but I have verified in the debugger that RenderColumns actually seems to retain and reflect any column reordering the user did across rebinds, and so now I need some more explcit way to force the radGrid to 'forget'.

-Ed
0
Kyle
Top achievements
Rank 1
answered on 09 Sep 2008, 02:44 PM
Ed,

Try setting EnableViewState to false and see if that gives you the desired affect that you are looking for.  Basically this setting will allow the grid to "forget" its settings on page postback.

Daniel,

Using GetColumnSafe returns nothing because whenever we access the column collection with auto generated columns, the column collection is nothing (count = 0).  Am I missing something?  Is the column collection always 0 when using auto generated columns?

Thanks,
Kyle
0
Kyle
Top achievements
Rank 1
answered on 09 Sep 2008, 02:50 PM
Daniel,

Sorry, It actually does work.  There was an error on my part.  However, why can we access the column using getColumn but the column collection of the grid has zero elements?  This seems strange to me.

Thanks,
Kyle
0
Ed
Top achievements
Rank 1
answered on 09 Sep 2008, 03:11 PM
Setting EnableViewState to false is throwing the baby out with the bathwater.  Then every postback will lose everything the user did.  I just want to throw away what they did if and only if they hit the reset button to get back to the original state. Imagine a user normally looks at a bunch of data, but they have lots of columns (involving scrolling horizontally) and they currently need to do some filtering and sorting based on a couple far-right columns and look at them side by side with one of the first few columns.  So they drag aroudn the columns to see the ones they need to compare at the moment next to each other, apply some sorts and filters, and do their work.  Now they are done and want to go back to the way the data is normally displayted.  During the above activity, they don;t want to lose the rearrangement every time they click on the screen (postback on rowclick is essential in this app).  They just want to return to the original state whe n they say so.

If ViewState is now the only way to do it (unlike with the old RadGrid for ASP.Net), then is their a way to one-time clear viewstate for the specific grid and then return the setting the viewstate enabled right after?

-Ed
Tags
Grid
Asked by
Kyle
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Kyle
Top achievements
Rank 1
Ed
Top achievements
Rank 1
Share this question
or