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

[Solved] Dynamic GridEditCommandColumn

2 Answers 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
macy
Top achievements
Rank 1
macy asked on 25 Feb 2010, 08:32 AM
Hi
I have build a RadGrid with dynamic Colums. When i push the Add or Edit Button i get an Error

Code example
Aspx:
    <form id="form1" runat="server">  
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
    </telerik:RadScriptManager> 
    <div> 
        <telerik:RadGrid ID="RadGridDataList" runat="server" AutoGenerateColumns="False" 
            GridLines="None" > 
            <MasterTableView EditMode="PopUp" CommandItemDisplay="Top" > 
            </MasterTableView> 
        </telerik:RadGrid> 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
            ConnectionString="<%$ ConnectionStrings:MT_LDHORep_NewTestConnectionString %>"   
            SelectCommand="SELECT [ROADMAP_PART_NAME], [ROADMAP_PART_DESC], [ROADMAP_PART_ID] FROM [DHO_ROADMAP_PART]">  
        </asp:SqlDataSource> 
    </div> 
    </form> 
 

Code behinde:
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                RadGridDataList.DataSourceID = "SqlDataSource1";  
                RadGridDataList.MasterTableView.DataKeyNames = new string[] { "ROADMAP_PART_ID" };  
 
                RadGridDataList.MasterTableView.Columns.Clear();  
 
                GridEditCommandColumn editColl = new GridEditCommandColumn();  
                //editColl.UniqueName = "EditButton";  
                editColl.ButtonType = GridButtonColumnType.ImageButton;  
                editColl.ItemStyle.Width = new Unit(1, UnitType.Percentage);  
                RadGridDataList.MasterTableView.Columns.Add(editColl);  
 
 
                GridBoundColumn boundColumn;  
                boundColumn = new GridBoundColumn();  
                RadGridDataList.MasterTableView.Columns.Add(boundColumn);  
                boundColumn.DataField = "ROADMAP_PART_NAME";  
                boundColumn.HeaderText = "Name";  
                boundColumn = new GridBoundColumn();  
                RadGridDataList.MasterTableView.Columns.Add(boundColumn);  
                boundColumn.DataField = "ROADMAP_PART_DESC";  
                boundColumn.HeaderText = "Description";  
 
            }  
        }  
 

What is wrong

Regards
Markus

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 25 Feb 2010, 10:53 AM
Hi Markus,

"To define the structure of a RadGrid control that is declared in the ASPX page, use the Page_Load event handler. Columns and detail table should be added to the corresponding collection first, before the values for their properties are set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection."

Please add the columns to the control collection before you set the properties.


Thanks,
Princy
0
macy
Top achievements
Rank 1
answered on 25 Feb 2010, 11:59 AM
Hi Princy

Thanks a lot. It works now.

Regrads Markus
Tags
Grid
Asked by
macy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
macy
Top achievements
Rank 1
Share this question
or