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

Dynamically add columns in radgrid

5 Answers 518 Views
Grid
This is a migrated thread and some comments may be shown as answers.
azhagu
Top achievements
Rank 1
azhagu asked on 20 Dec 2008, 08:50 AM
Hi

How to add templatecolumn and edittemplatecolumn dynamically for the existing radgrid. Please answer , It is very urgent.

Thanks in Advance

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 20 Dec 2008, 09:10 AM
Hello Azhagu,

Please examine the following link:
RadGrid columns programmatic creation

Let us know if you need further assistance.

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
azhagu
Top achievements
Rank 1
answered on 20 Dec 2008, 09:21 AM
Hi

Dynamically adding Radgrid columns,


1) Template Columns  --  How to add label within the template columns programatically

2) EditTemplate Columns  -- How to add textbox within the editemplate columns programatically

3)  Default all the label's should be shown.
 Edit -- When click edit that textbox should be shown.
 Save - All the entered values are stored in the db and retrive again.

Pls reply.

Thanks In Advance
0
Sebastian
Telerik team
answered on 20 Dec 2008, 09:25 AM
Hello azhagu,

You will find the information concerning all points at the bottom section of the same documenation topic.

Best,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
azhagu
Top achievements
Rank 1
answered on 20 Dec 2008, 09:37 AM
Hi
I used like that example, i created but it is throughing error like,

Element is neither a DataColumn nor a DataRelation for table ProgramMapping.


Herewith i have attached my coding.
Pls reply,

Radgrid Design:

<telerik:RadGrid AutoGenerateColumns="false" ID="rgIncentive" GridLines="Vertical" runat="server" Skin="WebBlue"
                                                                                Width="100%" PageSize="5" AllowSorting="true" AllowPaging="true" ClientSettings-Scrolling-UseStaticHeaders="true"
                                                                                AllowMultiRowSelection="true" ClientSettings-Selecting-AllowRowSelect="true"
                                                                                ClientSettings-Resizing-AllowColumnResize="true" ShowFooter="true" ClientSettings-Resizing-EnableRealTimeResize="true">
                                                                                <PagerStyle Mode="NextPrevAndNumeric" />
                                                                            </telerik:RadGrid> 
RadGrid Coding
function GridCreate()
{
DataTable dt = new DataTable();
            dt = MappingControl.CreateDt();            
            rgIncentive.DataSource = dt;
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName != "Per KWH Customer Incentive" && dc.ColumnName != "Per KWH SP Incentive" && dc.ColumnName != "CAP")
                {
                    GridBoundColumn boundColumn1 = new GridBoundColumn();
                    boundColumn1.DataField = dc.ColumnName.ToString();
                    boundColumn1.UniqueName = dc.ColumnName.ToString();
                    boundColumn1.HeaderText = dc.ColumnName.ToString();
                    rgIncentive.MasterTableView.Columns.Add(boundColumn1);
                }
                else
                {
                    string templateColumnName = dc.ColumnMapping.ToString();
                    GridTemplateColumn templateColumn = new GridTemplateColumn();
                    templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
                    templateColumn.HeaderText = templateColumnName;
                    //templateColumn.EditItemTemplate = new MyEditTemplate(templateColumnName);
                    rgIncentive.MasterTableView.Columns.Add(templateColumn);

                    //GridTemplateColumn templateColumn = new GridTemplateColumn();
                    //string templateColumnName = dc.ColumnName.ToString();
                    //templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
                    //templateColumn.EditItemTemplate = new MyTemplate(templateColumnName);
                    //templateColumn.HeaderText = templateColumnName;
                    //rgIncentive.MasterTableView.Columns.Add(templateColumn);
                }
            }
            GridEditCommandColumn ec = new GridEditCommandColumn();
            ec.HeaderText = "Edit";
            ec.ButtonType = GridButtonColumnType.ImageButton;
            ec.InsertImageUrl = "";
            ec.UpdateText = "Update";
            ec.CancelText = "Cancel";
            rgIncentive.MasterTableView.Columns.Add(ec);
            rgIncentive.MasterTableView.Width = Unit.Percentage(100);
            rgIncentive.DataBind();
}

Interface

private class MyTemplate : ITemplate
    {
        protected LiteralControl lControl;
        protected RequiredFieldValidator validatorTextBox;
        protected HyperLink searchGoogle;
        protected TextBox textBox;
        protected CheckBox boolValue;
        private string colname;
        public MyTemplate(string cName)
        {
            colname = cName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            lControl = new LiteralControl();
            lControl.ID = "lControl";
            lControl.DataBinding += new EventHandler(lControl_DataBinding);
            textBox = new TextBox();
            textBox.ID = "templateColumnTextBox";
            validatorTextBox = new RequiredFieldValidator();
            validatorTextBox.ControlToValidate = "templateColumnTextBox";
            validatorTextBox.ErrorMessage = "*";
            //searchGoogle = new HyperLink();
            //searchGoogle.ID = "searchGoogle";
            //searchGoogle.DataBinding += new EventHandler(searchGoogle_DataBinding);
            //boolValue = new CheckBox();
            //boolValue.ID = "boolValue";
            //boolValue.DataBinding += new EventHandler(boolValue_DataBinding);
            //boolValue.Enabled = false;
            Table table = new Table();
            TableRow row1 = new TableRow();
            TableRow row2 = new TableRow();
            TableCell cell11 = new TableCell();
            TableCell cell12 = new TableCell();
            //TableCell cell21 = new TableCell();
            //TableCell cell22 = new TableCell();
            row1.Cells.Add(cell11);
            row1.Cells.Add(cell12);
            //row2.Cells.Add(cell21);
            //row2.Cells.Add(cell22);
            table.Rows.Add(row1);
            table.Rows.Add(row2);
            cell11.Text = colname + ": ";
            cell12.Controls.Add(lControl);
            //cell21.Text = "Search Google for: ";
            //cell22.Controls.Add(searchGoogle);
            container.Controls.Add(textBox);
            container.Controls.Add(validatorTextBox);
            container.Controls.Add(table);
            container.Controls.Add(new LiteralControl("<br />"));
            //container.Controls.Add(boolValue);
        }
        void boolValue_DataBinding(object sender, EventArgs e)
        {
            CheckBox cBox = (CheckBox)sender;
            GridDataItem container = (GridDataItem)cBox.NamingContainer;
            cBox.Checked = (bool)((DataRowView)container.DataItem)["Bool"];
        }
        void searchGoogle_DataBinding(object sender, EventArgs e)
        {
            HyperLink link = (HyperLink)sender;
            GridDataItem container = (GridDataItem)link.NamingContainer;
            link.Text = ((DataRowView)container.DataItem)[colname].ToString();
            link.NavigateUrl = "http://www.google.com/search?hl=en&q=" +
                  ((DataRowView)container.DataItem)["ContactName"].ToString() +
                  "&btnG=Google+Search";
        }
        public void lControl_DataBinding(object sender, EventArgs e)
        {
            LiteralControl l = (LiteralControl)sender;
            GridDataItem container = (GridDataItem)l.NamingContainer;
            l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";
        }
    }

Thanks in advance
0
azhagu
Top achievements
Rank 1
answered on 20 Dec 2008, 10:28 AM
HI,
i used that coding.

But the textbox value is not binding correctly in that.

Thanks In Advance

Tags
Grid
Asked by
azhagu
Top achievements
Rank 1
Answers by
Daniel
Telerik team
azhagu
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or