Using server side binding for GridTemplateColumns

2 posts, 1 answers
  1. William
    William avatar
    13 posts
    Member since:
    Oct 2018

    Posted 25 Oct 2018 Link to this post

    I'm using the advanced data binding technique from https://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/simple-vs-advanced/defaultvb.aspx .  However, I need to use GridTemplateColumns to format some of the columns ( <%#String.Format("{0} - {1}", Eval("user"), Eval("name")) %> ) and because I'm using these, nothing comes up.  Is there a way to do this?  If not, is there a better solution?  I did copy the example exactly, but used my datatable instead and it came up, but gave me every single column.  I know I can remove columns, but this doesn't allow me to format columns like I do with GridTemplateColumns.

  2. Answer
    Attila Antal
    Admin
    Attila Antal avatar
    302 posts

    Posted 30 Oct 2018 Link to this post

    Hi William, 

    It would be helpful if you could give us a little more details about the grid settings/markup/code behind. The following code snippet is an example that is bound to DataTable with Dummy data and evaluating of the field is working well.

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0"
        GridLines="None" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID">
            <Columns>
                 <telerik:GridTemplateColumn HeaderText="My Template Column" UniqueName="MyTemplateColumn">
                     <ItemTemplate>
                         <telerik:RadLabel ID="RadLabel1" runat="server"
                             Text='<%#String.Format("You order with id {0} was received at {1}", Eval("OrderID"), Eval("OrderDate")) %>'>
                         </telerik:RadLabel>
                     </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C# - Code behind:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetGridSource();
    }
    private DataTable GetGridSource()
    {
        DataTable dataTable = new DataTable();
     
        DataColumn column = new DataColumn();
        column.DataType = Type.GetType("System.Int32");
        column.ColumnName = "OrderID";
        dataTable.Columns.Add(column);
     
        column = new DataColumn();
        column.DataType = Type.GetType("System.DateTime");
        column.ColumnName = "OrderDate";
        dataTable.Columns.Add(column);
     
        column = new DataColumn();
        column.DataType = Type.GetType("System.Decimal");
        column.ColumnName = "Freight";
        dataTable.Columns.Add(column);
     
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "ShipName";
        dataTable.Columns.Add(column);
     
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "ShipCountry";
        dataTable.Columns.Add(column);
     
        DataColumn[] PrimaryKeyColumns = new DataColumn[1];
        PrimaryKeyColumns[0] = dataTable.Columns["OrderID"];
        dataTable.PrimaryKey = PrimaryKeyColumns;
     
        for (int i = 0; i <4; i++)
        {
            DataRow row = dataTable.NewRow();
            row["OrderID"] = i + 1;
            row["OrderDate"] = DateTime.Now;
            row["Freight"] = (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01;
            row["ShipName"] = "Name " + (i + 1);
            row["ShipCountry"] = "Country " + (i + 1);
     
            dataTable.Rows.Add(row);
        }
     
        return dataTable;
    }

    Ultimately you can check out the attached project to see it in action. (Telerik assemblies are excluded to keep the attachment size at minimum).

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top