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

[Solved] Unable to cast object of type 'Telerik.Web.UI.GridBoundColumn' to type 'System.Web.UI.WebControls.DataControlField'.

1 Answer 296 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
shahid Aleem
Top achievements
Rank 1
shahid Aleem asked on 23 Feb 2010, 05:42 PM
 
This is my grid view code.... 
 
<columns> 
<telerik:GridBoundColumn DefaultInsertValue="" UniqueName="Code" DataField="Code" 
                                HeaderText="Code"
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="Employee" DataField="Employee" 
                                HeaderText="Employee"
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="Address" DataField="Address" 
                                HeaderText="Address"
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DefaultInsertValue="" UniqueName="Location" DataField="Location" 
                                HeaderText="Location"
</columns> 
 
 
And my checkboxlist contains following.... 
 
<asp:CheckBoxList ID="CheckBoxListThird" runat="server" RepeatColumns="5" Width="100%"
                        <asp:ListItem Enabled="False" Selected="True" Value="Code">Site ID</asp:ListItem> 
                        <asp:ListItem Selected="True">Employee</asp:ListItem> 
                        <asp:ListItem Selected="True">Address</asp:ListItem> 
                        <asp:ListItem Selected="True">Location</asp:ListItem> 
</asp:CheckBoxList> 
 
 
In my code behind i am trying something like this.... 
 
 
foreach (DataControlField col in RadGrid.Columns) 
                { 
                    if (col.HeaderText.ToUpper() == CheckBoxListFirst.Items[i].Text.ToUpper()) 
                        col.Visible = CheckBoxListFirst.Items[i].Selected; 
                } 
 
But It is giving me error.... This is just an example i have given i have in my listbox some 60 items.... 
 
so i want to come the headertext and item text of grid and checkboxlist.... My scenario fits in this way only..... 
 
Please tell me how to achevie it.... 
 
 
 
 
 
Hello,

i have a CheckBoxList Control in my page. When User selects or checks the check box i want to display only those selected Items in the grid.....

1 Answer, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 24 Feb 2010, 07:05 AM
Hello Shahid Aleem,

Try the code snippet below in order to show/hide the grid columns based on ChekBoxList checked.
 
C#:
 
    protected void CheckBoxListThird_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        foreach (ListItem listItem in CheckBoxListThird.Items) 
        { 
            if (listItem.Text != "Site ID"
            { 
                if (listItem.Selected) 
                { 
                    RadGrid1.MasterTableView.GetColumn(listItem.Text).Visible = true
                    // RadGrid1.MasterTableView.GetColumn(listItem.Text).HeaderText = listItem.Text.ToUpper(); // Set the HeaderText 
                } 
                else 
                { 
                    RadGrid1.MasterTableView.GetColumn(listItem.Text).Visible = false
                } 
            } 
        } 
        RadGrid1.MasterTableView.Rebind(); 
    } 

-Shinu.
Tags
General Discussions
Asked by
shahid Aleem
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or