Hide webgrid columns, defined in aspx, programmatically

3 replies. Last post: May 26, 2013 9:50 PM by Bernard Xiang
Tags :
  • New Discussion
  • New Question
  • New Product Feedback

I have webgrid with all columns that can be contained in datasource, defined in markup. I don't know in advance which columns will come from database so I have to have them all. If I add them from code behind(which old version of application did) I'd have over 2000 lines of code, and now I have about 160 of them because that's how many possible columns exist. I am binding grid to datasource, and of course, if column defined in webgrid doesn't exist in datasource, databinding fails. So what I'm doing in PrepareDataBinding event is check for all columns in grid, if they have their pair in datasource. If not the column is removed from grid's column collection.


For Each col As WebGridColumn In gridZaposleni.RootTable.Columns
 colName = col.Name
 colMember = col.DataMember
 If Not dt.Columns.Contains(colMember) Then
 gridZaposleni.RootTable.Columns.Remove(col)
 End If
 Next

 However, this doesn't work because the application fails as it comes to first column that doesn't have it's pair in datasource:

"Can't find DataColumn with name 'Company' in the specified datasource".

I also tried, instead of removing column, setting it's Bound and Visible properties to false. That works, but then I have the grid loading 160 columns, and when I have grouped col's, expanding them takes ages.

There is option to bind grid dynamically to datasource and make it create all columns by itself, but this doesn't work for me because, some columns have formating on them like width, forecolor etc.

My question is - why isn't my first option, with removing columns, working, and how to solve this problem?

All times are GMT -5. The time now is 12:30 AM.
Previous Next