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

Force Columns to be recreated on Postback

4 Answers 125 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ReneF
Top achievements
Rank 1
ReneF asked on 04 Aug 2008, 12:38 PM

I am trying to show different dataViews for diferent users with a DropDownList
How do I hide Columns on a  DropdownList selectedIndex Changed? 

Regards

Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

//this works well
LoadDefaultGridView();

}

}

//Event fires but columns are not updated!!
protected
void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)

{

if (e.Column is GridBoundColumn)

{

List<MyDataClass> currentListItems = MyDataController.getListItemForSelectedView(DropDownList1.SelectedIndex);

foreach (MyDataClass.View view in currentListItems)

{

if (view.Name == e.Column.HeaderText)

{

e.Column.Display =

true;

e.Column.HeaderText = view.LabelText;

break;

}

else

 

{

e.Column.Display =

false;

}

}

}

}

 

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

LoadSelectedGridView();

}

private void LoadDefaultGridView()

{

//Display only 5 colums of radGrid

 

}

private void LoadSelectedGridView()

{

//Display 8 Columns of radGrid

 

}

4 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 04 Aug 2008, 12:56 PM
Hello Rene,

To see more information along the lines of the functionality that you are pursuing, please refer to the following article, which contains additional information on the approach of dynamically altering the controls structure.
I hope this helps.

Sincerely yours,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
ReneF
Top achievements
Rank 1
answered on 04 Aug 2008, 01:28 PM

Hi Yavor,

I read your article. The  differences to your article is: I am using a List<> as Datasource and AutogenerateColumns=true.

grid.DataSourceID = "AccessDataSource1"; //MyList();
grid.MasterTableView.AutoGenerateColumns = false;//true;

//strange EnableViewState is set to false but The grid is always displaying initial columns
/from firts page_load
grid.EnableViewState =
false;
grid.MasterTableView.EnableColumnsViewState =
false;

Even ColumnCreated(object sender, GridColumnCreatedEventArgs e)  event fires goes thrue but columnsheader are not updated.

Thanks for your help.

0
Accepted
Yavor
Telerik team
answered on 05 Aug 2008, 10:58 AM
Hello ReneF,

Basically, there are two possible approaches in this case.
The first is to recreate the control structure dynamically on each pageInit, based on the value of the Dropdown. Alternatively, you can simply show all the columns in the control (the max number of columns which could appear in the control), and simply hide conditionally the columns which are not needed. This can be done in the PreRender event handler, by iterating through the RadGrid1.MasterTableView.RenderColumns collection.
In either case, the datasource of the control is not very important, as far as this capability is discussed.
Let me know if I am leaving something out.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
ReneF
Top achievements
Rank 1
answered on 05 Aug 2008, 03:38 PM
Hi Yavor,
I solved the problem similar to your suggestions.
I get the column collection on Page_Load and set the
column display properties.
foreach (string myHeaderText in myHeaderTextCollection)  
                {  
                    foreach (Telerik.Web.UI.GridColumn column in RadGrid1.MasterTableView.Columns)  
                    {  
                        if (column.HeaderText == myHeaderText)  
                        {  
                            column.Display = true;  
                            break;  
                        }  
                        else  
                        {  
                            column.Display = false;  
                             
                        }  
                    }  
                } 
Thanks for your help
Tags
Grid
Asked by
ReneF
Top achievements
Rank 1
Answers by
Yavor
Telerik team
ReneF
Top achievements
Rank 1
Share this question
or