Hi,
I am trying to create a grid programatically using client side databinding and it works fine when using GridBoundColumns but nothing appears when I use GridTemplateColumns. I have followed all the instructions from telerik(http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html).
I have pasted my code below.
In the PopulateResultGridColumns method right now it is adding GridBoundColumns. If we comment the region "GridBoundColumns" and uncomment the region "GridTemplateColumns" which adds GridTemplateColumns instead, it doesn't work.
This is a simplified example, and we have complex templates from GridView to be replaced with telerik grid.
Please let me know if client side databinding does work with programmatic creation of template fields.
Thanks,
Tilak
Below is my aspx:
This is my code behind:
I am trying to create a grid programatically using client side databinding and it works fine when using GridBoundColumns but nothing appears when I use GridTemplateColumns. I have followed all the instructions from telerik(http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html).
I have pasted my code below.
In the PopulateResultGridColumns method right now it is adding GridBoundColumns. If we comment the region "GridBoundColumns" and uncomment the region "GridTemplateColumns" which adds GridTemplateColumns instead, it doesn't work.
This is a simplified example, and we have complex templates from GridView to be replaced with telerik grid.
Please let me know if client side databinding does work with programmatic creation of template fields.
Thanks,
Tilak
Below is my aspx:
| <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridExample._Default" %> |
| <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head runat="server"> |
| <title></title> |
| </head> |
| <body> |
| <form id="form1" runat="server"> |
| <div> |
| <asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager> |
| <telerik:RadGrid ID="ResultsGrid" runat="server" GridLines="None" AllowFilteringByColumn="true" |
| AllowSorting="true" AllowPaging="true" PageSize="10" EnableViewState="true"> |
| <ClientSettings> |
| <DataBinding SelectMethod="FindItemsAndCount" /> |
| </ClientSettings> |
| <MasterTableView Width="100%"> |
| </MasterTableView> |
| <PagerStyle Mode="NextPrevAndNumeric" /> |
| </telerik:RadGrid> |
| <asp:PlaceHolder ID="phGrid" runat="server"></asp:PlaceHolder> |
| </div> |
| </form> |
| </body> |
| </html> |
This is my code behind:
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Web; |
| using System.Web.UI; |
| using System.Web.UI.WebControls; |
| using Telerik.Web.UI; |
| using System.Web.Services; |
| using System.Data; |
| namespace GridExample |
| { |
| public partial class _Default : System.Web.UI.Page |
| { |
| protected override void OnInit(EventArgs e) |
| { |
| base.OnInit(e); |
| PopulateResultGridColumns(this.ResultsGrid); |
| //RadGrid grid = new RadGrid(); |
| //grid.AllowPaging = true; |
| //grid.AutoGenerateColumns = false; |
| //grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric; |
| //PopulateResultGridColumns(grid); |
| //grid.ClientSettings.DataBinding.SelectMethod = "FindItemsAndCount"; |
| //grid.ClientSettings.DataBinding.Location = "Default.aspx"; |
| //phGrid.Controls.Add(grid); |
| //grid.Rebind(); |
| } |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| ResultsGrid.ClientSettings.DataBinding.Location = "Default.aspx"; |
| ResultsGrid.Rebind(); |
| } |
| protected void PopulateResultGridColumns(RadGrid grid) |
| { |
| List<Field> fields = new List<Field> |
| { |
| new Field { DisplayName = "ID", ColumnName = "ID" }, |
| new Field { DisplayName = "Name", ColumnName = "Name" }, |
| new Field { DisplayName = "Description", ColumnName = "Description" }, |
| new Field { DisplayName = "Location", ColumnName = "Location" }, |
| new Field { DisplayName = "Name", ColumnName = "Name" } |
| }; |
| foreach (var field in fields) |
| { |
| #region GridBoundColumns |
| GridBoundColumn col = new GridBoundColumn(); |
| grid.MasterTableView.Columns.Add(col); |
| col.DataField = field.ColumnName; |
| col.HeaderText = field.DisplayName; |
| if (field.ColumnName == "ID") |
| col.DataType = Type.GetType("System.Int32"); |
| #endregion |
| #region GridTemplateColumns |
| //GridTemplateColumn tfield = new GridTemplateColumn(); |
| //grid.MasterTableView.Columns.Add(tfield); |
| //tfield.ItemTemplate = new BoundTemplate(System.Web.UI.WebControls.ListItemType.Item, field.ColumnName); |
| //tfield.DataField = field.ColumnName; |
| //tfield.HeaderText = field.DisplayName; |
| //if (field.ColumnName == "ID") |
| // tfield.DataType = Type.GetType("System.Int32"); |
| #endregion |
| } |
| } |
| [WebMethod(EnableSession = true)] |
| public static Dictionary<String, Object> FindItemsAndCount( |
| int startRowIndex, int maximumRows, |
| List<GridSortExpression> sortExpression, |
| List<GridFilterExpression> filterExpression) |
| { |
| System.Data.DataTable dt = new System.Data.DataTable(); |
| dt.Columns.Add("ID", Type.GetType("System.Int32")); |
| dt.Columns.Add("Name"); |
| dt.Columns.Add("Description"); |
| dt.Columns.Add("Location"); |
| for (int i = 0; i < 35; i++) |
| { |
| System.Data.DataRow dr = dt.NewRow(); |
| dr["ID"] = i; |
| dr["Name"] = "Name" + i; |
| dr["Description"] = "Description" + i; |
| dr["Location"] = "Location" + i; |
| dt.Rows.Add(dr); |
| } |
| return new Dictionary<String, Object> |
| { |
| { "Data", RowsToDictionary(dt, dt.Select(string.Format(" ID > {0} and ID < {1}", startRowIndex.ToString(), (startRowIndex + maximumRows).ToString()))) }, |
| { "Count", dt.Rows.Count } |
| }; |
| } |
| private static List<Dictionary<string, object>> RowsToDictionary(DataTable table, DataRow[] rows) |
| { |
| List<Dictionary<string, object>> objs = |
| new List<Dictionary<string, object>>(); |
| foreach (DataRow dr in rows) |
| { |
| Dictionary<string, object> drow = new Dictionary<string, object>(); |
| for (int i = 0; i < table.Columns.Count; i++) |
| { |
| drow.Add(table.Columns[i].ColumnName, dr[i]); |
| } |
| objs.Add(drow); |
| } |
| return objs; |
| } |
| } |
| [Serializable] |
| public class Field |
| { |
| public String ColumnName { get; set; } |
| public String DisplayName { get; set; } |
| } |
| public class BoundTemplate : System.Web.UI.ITemplate |
| { |
| public ListItemType templateType; |
| public string columnName, sortExpression; |
| public BoundTemplate(ListItemType type, string colName) |
| { |
| templateType = type; |
| this.columnName = colName; |
| } |
| public void InstantiateIn(Control container) |
| { |
| switch (templateType) |
| { |
| case ListItemType.Item: |
| case ListItemType.AlternatingItem: |
| Label dataLabel = new Label(); |
| dataLabel.Text = columnName; |
| container.Controls.Add(dataLabel); |
| dataLabel.DataBinding += new EventHandler(Item_DataBinding); |
| break; |
| default: |
| break; |
| } |
| } |
| public void Item_DataBinding(object sender, System.EventArgs e) |
| { |
| Label lbl = (Label)sender; |
| GridDataItem container = (GridDataItem)lbl.NamingContainer; |
| string value = string.Empty; |
| value = DataBinder.Eval(container.DataItem, columnName).ToString(); |
| lbl.Text = value; |
| } |
| } |
| } |