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

[Solved] Applying specific ColumnEditor control for dynamic columns

2 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Babu Mannaravalappil
Top achievements
Rank 1
Babu Mannaravalappil asked on 05 Nov 2009, 08:14 PM
Hi,

I have a statically declared but dynamically created Grid.  The columns and their datatypes are not known until runtime.  So, I create regular GridBoundColumn at runtime.  Some columns are numeric and some columns need to be dropdowns, checkboxes etc.  I have all the code to create the boundcolumns and display it and edit it.  I guess I need to replace the auto-generated column editors in CreateColumnEditor event.  Is there a sample I can base my code upon?  Because the following code throws me an error "Object reference not set to an instance of an object".

Here is my code to create the columns.
    protected void DefineColumnsForGrid(RadGrid grid, DataTable tbl)  
    {  
        grid.Columns.Clear();  
        FormsIdentity user = (FormsIdentity)HttpContext.Current.User.Identity;  
        if (user == null)  
            return;  
        for (int i = 0; i < tbl.Columns.Count; i++)  
        {  
            GridBoundColumn col = new GridBoundColumn();  
            col.HeaderText = tbl.Columns[i].ColumnName;  
            col.DataField = tbl.Columns[i].ColumnName;  
            col.UniqueName = tbl.Columns[i].ColumnName;  
            col.DataType = tbl.Columns[i].DataType;  
            col.ReadOnly = true;    // isEditable  
            grid.Columns.Add(col);  
        }  
    }  
 


And here my code for CreateColumnEditor event:
    protected void rgForecast_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e)  
    {  
        if (e.Column is GridBoundColumn)  
        {  
            if ((e.Column as GridBoundColumn).DataType == typeof(System.Decimal))  
            {  
                (e.Column as GridBoundColumn).ColumnEditorID = (e.Column as GridBoundColumn).UniqueName + "1";  
                e.ColumnEditor = new GridNumericColumnEditor(new GridNumericColumn());  
            }  
        }  
    }  
 

Please help.

Babu.

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 06 Nov 2009, 06:34 AM
Hi,

You need to add the columns and detail table  to the grid 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.

C#
 protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            GridBoundColumn col = new GridBoundColumn(); 
            RadGrid2.Columns.Add(col); 
            col.HeaderText = "Address"
            col.DataField = "Address"
            col.UniqueName = "Address"
          
            GridEditCommandColumn col2 = new GridEditCommandColumn(); 
            RadGrid2.Columns.Add(col2); 
 
 
 
        } 
         
    } 

I have attached a custom editor using the samples from the help document using this scenario and its is working fine for me .

Thanks,
Shinu
0
Babu Mannaravalappil
Top achievements
Rank 1
answered on 16 Nov 2009, 05:31 PM
Thanks Shinu,

THAT was my problem.  Once I added the columns before I started setting it's properties, things are working fine.

Babu.
Tags
Grid
Asked by
Babu Mannaravalappil
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Babu Mannaravalappil
Top achievements
Rank 1
Share this question
or