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

RadGrid created programmatically with custom column

1 Answer 234 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Francis
Top achievements
Rank 1
Francis asked on 05 Dec 2014, 12:03 PM
Hi,

I have a simple page where I create a grid programmatically. I have to put custom column on this grid, so I simulate it in using simple CustomBoundColumn who extend GridBoundColumn.
When I manipulate the grid (paging, filtering, ...), I give this message :

Cannot create column with the specified type name: CustomBoundColumn

Here is my aspx file :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicGrid.aspx.cs" Inherits="DEmosTelerik.DynamicGrid" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server"></telerik:RadScriptManager>
    <div>
        <asp:PlaceHolder runat="server" ID="PlaceHolder"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>


And my aspx.cs file :

using System;
using System.Collections.Generic;
 
using Telerik.Web.UI;
 
namespace DEmosTelerik
{
    public partial class DynamicGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadGrid grid = new RadGrid();
            grid.ID = "grid";
            grid.AllowPaging = true;
            grid.PageSize = 2;
            grid.MasterTableView.AutoGenerateColumns = false;
            grid.EnableViewState = true;
            grid.AllowFilteringByColumn = true;
            grid.PagerStyle.AlwaysVisible = true;
            grid.AllowSorting = true;
            grid.NeedDataSource += Grid_NeedDataSource;
 
            PlaceHolder.Controls.Add(grid);
            if (!IsPostBack)
            {
                CustomBoundColumn boundColumn;
 
                boundColumn = new CustomBoundColumn();
                grid.MasterTableView.Columns.Add(boundColumn);
                boundColumn.DataField = "Numero";
                boundColumn.HeaderText = "Numéro";
 
                boundColumn = new CustomBoundColumn();
                grid.MasterTableView.Columns.Add(boundColumn);
                boundColumn.DataField = "Nom";
                boundColumn.HeaderText = "Nom";
                boundColumn.CurrentFilterFunction = GridKnownFunction.Contains;
                boundColumn.DataFormatString = "<nobr>{0}<nobr>";
                boundColumn.AutoPostBackOnFilter = true;
                boundColumn.ShowFilterIcon = false;
 
                boundColumn = new CustomBoundColumn();
                grid.MasterTableView.Columns.Add(boundColumn);
                boundColumn.DataField = "Prenom";
                boundColumn.HeaderText = "Prénom";
                boundColumn.CurrentFilterFunction = GridKnownFunction.Contains;
                boundColumn.DataFormatString = "<nobr>{0}<nobr>";
                boundColumn.AutoPostBackOnFilter = true;
                boundColumn.ShowFilterIcon = false;
            }
        }
 
        protected void Grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            (sender as RadGrid).DataSource = SearchEleves();
        }
 
        private List<Eleve> SearchEleves()
        {
            List<Eleve> eleves = new List<Eleve>();
            eleves.Add(new Eleve { Numero = 12345, Nom = "Garnier", Prenom = "Francis" });
            eleves.Add(new Eleve { Numero = 1457, Nom = "Caillet", Prenom = "Jonas" });
            eleves.Add(new Eleve { Numero = 5487, Nom = "Delli Gatti", Prenom = "José" });
            eleves.Add(new Eleve { Numero = 8596, Nom = "Stoppani", Prenom = "Alexandre" });
            eleves.Add(new Eleve { Numero = 8529, Nom = "Benoit", Prenom = "Roland" });
            eleves.Add(new Eleve { Numero = 5589, Nom = "Paupe", Prenom = "Daniel" });
            eleves.Add(new Eleve { Numero = 25996, Nom = "Bellanca", Prenom = "David" });
 
            return eleves;
        }
    }
 
    public class Eleve
    {
        public decimal Numero { get; set; }
 
        public string Nom { get; set; }
        public string Prenom { get; set; }
    }
 
    public class CustomBoundColumn : GridBoundColumn
    {
         
    }
}

What is wrong ?


Thank you !

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 10 Dec 2014, 08:52 AM
Hello Francis,

RadGrid does not support columns that inherits the predefined columns. The only possible way for having such inherited columns would be to create the grid entirely within the Page's Init event, where the grid will not have to create its structure internally after the initial postback:
protected void Page_Init(object sender, EventArgs e)
{
    RadGrid grid = new RadGrid();
    grid.ID = "grid";
    grid.AllowPaging = true;
    grid.PageSize = 2;
    grid.MasterTableView.AutoGenerateColumns = false;
    grid.EnableViewState = true;
    grid.AllowFilteringByColumn = true;
    grid.PagerStyle.AlwaysVisible = true;
    grid.AllowSorting = true;
    grid.NeedDataSource += Grid_NeedDataSource;
 
    CustomBoundColumn boundColumn;
 
    boundColumn = new CustomBoundColumn();
    boundColumn.DataField = "Numero";
    boundColumn.HeaderText = "Numéro";
    grid.MasterTableView.Columns.Add(boundColumn);
 
    boundColumn = new CustomBoundColumn();
    boundColumn.DataField = "Nom";
    boundColumn.HeaderText = "Nom";
    boundColumn.CurrentFilterFunction = GridKnownFunction.Contains;
    boundColumn.DataFormatString = "<nobr>{0}<nobr>";
    boundColumn.AutoPostBackOnFilter = true;
    boundColumn.ShowFilterIcon = false;
    grid.MasterTableView.Columns.Add(boundColumn);
 
    boundColumn = new CustomBoundColumn();
    boundColumn.DataField = "Prenom";
    boundColumn.HeaderText = "Prénom";
    boundColumn.CurrentFilterFunction = GridKnownFunction.Contains;
    boundColumn.DataFormatString = "<nobr>{0}<nobr>";
    boundColumn.AutoPostBackOnFilter = true;
    boundColumn.ShowFilterIcon = false;
    grid.MasterTableView.Columns.Add(boundColumn);
 
    PlaceHolder.Controls.Add(grid);
}

Generally, for custom columns you should use GridTemplateColumn with classes that implements the ITemplate interface. Detailed information and example on this matter could be found in the following article:
Hope this helps.
 


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Francis
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or