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

Dynamic RadGrid with DropDownColumn

4 Answers 279 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allen
Top achievements
Rank 1
Allen asked on 18 Feb 2014, 03:36 PM

I am creating a Dynamically built RadGrid. I have to do so because I am generating columns based on how many dates my sql query returns. I am placing the specific dates as header column groups. Under each date I am trying to create a dropdowncolumn that has a different datasource than the radGrid.
The problem is creating it dynamically. I thought about using an item template but how could I place a dropdown in the item template for each drop down that i need to create, which is two per date.  
How do i bind the datasource for the DropDownColumn from code behind when the entire grid is dynamically built.


for (int t = 0; t < datecount; t++)
        {
            int utacount = Convert.ToInt32(getDrill.Rows[t][3].ToString());

            for (int p = 0; p < datecount; p++)
            {

                GridDropDownColumn ddc = new GridDropDownColumn();

                int b = p + 1;

                ddc.UniqueName = "ddc" + p + s;
                if (p - 1 < utacount)
                {
                    string name = "uta" + p + s;
                    ddc.ColumnGroupName = name;
                    //ddc.DataField = "strStatus";
                    //ddc.ListTextField = "strStatus";
                    ////GridEditableItem editedItem = ddc as GridEditableItem;
                    ////GridEditManager editman = editedItem.EditManager;
                    ////GridDropDownColumnEditor editor = editman.GetColumnEditor(name) as GridDropDownColumnEditor;
                    ////editor.DataSource = getDrill;
                    ////editor.DataBind();

                }
                grid.MasterTableView.Columns.Add(ddc);
            }

            s++;
        }

4 Answers, 1 is accepted

Sort by
0
Allen
Top achievements
Rank 1
answered on 18 Feb 2014, 03:52 PM
I have tried onItemDataBound event as well but it requires Arguments and i am not sure how to send those from code behind.

I tried

grid.ItemDataBound = grid_itemDataBound();

I am not sure how to send the arguments that the event requires when i give the grid this
0
Viktor Tachev
Telerik team
answered on 21 Feb 2014, 11:22 AM
Hi Allen,

In order to use the ItemDataBound event to populate the data in the DropDownList you need to attach a handler for the event.

RadGrid grid = new RadGrid();
 
grid.ItemDataBound += grid_ItemDataBound;

In the handler you could set the DataSource for the dropdown control. You could use a GridDropDownColumn or a GridTemplateColumn in your scenario. Note that when creating template columns in RadGrid programmatically you need to use the approach described in this article. If you need additional information on how to customize a GridDropDownColumn it is available in this article.

For convenience I am attaching a sample project illustrating how you could set the data source for a dropdown control in the ItemDataBound handler of RadGrid.

Regards,
Viktor Tachev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
0
Gajanan
Top achievements
Rank 2
answered on 17 Aug 2015, 10:08 AM
Dear all,
My requirement is Bind the grid dynamic , and add the controls like Text Box And rad Como box at run time by Item value.
so far have done the control adding and now my grid is ready to take values from user , 
but when user selected value from rad combo box (dynamic added control in grid), and click the save button i cannot find the Rad Combo box 
pls suggest me the way how to get the values for dynamically added controls in Rad-grid on button click.
below are the steps i followed
1. Added Grid in Aspx. with Autogenerated Column = true
2. On Item created event added dynamic controls in Grid. 
3. bind the data to ad combo box (dynamic added control in grid) 
4. on save Button i have to save the selected values from rad combo box (dynamic added control in grid), to data base
in above step 1 to 3 are done , but step 4 making me trouble .

below is code for button click.
protected void btnSaveAll_Click(object sender, EventArgs e)
{
    Questionnaire MyQue = Questionnaire.getQuestionnaire(mQuestionnaireid); 
    ArrayList ColumnLst = GetColumnList();
                Dictionary<string,string> RowSplitValue = new Dictionary<string,string>();
                foreach (GridEditableItem Item in rgPivotQuestionnaireResult.MasterTableView.Items)
                {
                    foreach (GridColumn Column in rgPivotQuestionnaireResult.MasterTableView.RenderColumns)
                    {
                        if (ColumnLst.Contains(Column.UniqueName))
                        {
                            //RowSplitValue = new Dictionary<string, string>(Item.SavedOldValues[Column.UniqueName]);
                            if (Item[Column].Controls.Count > 0)
                            {
                                Control ItemControle= new Control();
                                ItemControle = Item[Column].Controls[0];
                                if (ItemControle is TextBox)
                                {
                                    TextBox TxtBx = (TextBox)Item[Column].Controls[0];
                                }
                                if (Item[Column].Controls.Count > 1)
                                {
                                    ItemControle = Item[Column].Controls[1];
                                    if (ItemControle is RadComboBox)
                                    {
                                        RadComboBox RCB = (RadComboBox)Item[Column].Controls[1];
                                          MyQue .Answervalue = RCB.SelectedValue;
                                    }
                                }
                            }
                        }
                    }
                }
   MyQue .Save();
}
0
Viktor Tachev
Telerik team
answered on 18 Aug 2015, 01:00 PM
Hi Gajanan,

If you would like to customize the default editor for a column you should handle the CreateColumnEditor event for RadGrid. In the handler you can replace the default ColumnEditor with a different one. The approach is described in the following article.



Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Allen
Top achievements
Rank 1
Answers by
Allen
Top achievements
Rank 1
Viktor Tachev
Telerik team
Gajanan
Top achievements
Rank 2
Share this question
or