Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Dynamically define rad grid using code behind in c# grid - ui for asp
1. 9/2/2015 Dynamically define Rad Grid using Code Behind in C# Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/dynamicallydefineradgridusingcodebehindinc 1/8
DynamicallydefineRadGridusing
CodeBehindinC#
Resources Buy Try
Telerik Forums / UI for ASP.NET AJAX Forum / Grid /
7 posts, 0 answers
UI for ASP.NET AJAXñAJAX
Post a reply ø Feed for this thread
Muhamma
d
4 posts
Member
since:
Dec 2013
Link to this postPosted 12 May 2014
I want to add Rad Grid Dynamically using Code Behind.with Dynamic
Columns 1 Columns should be Drop Down , 1 Column Should be Date picker,
1 Column Should be Text field.these Dynamic Columns should be Shown in
Edit mode when Edit button is Clicked.while Showing Data just Data should
be shown like data in label.
how can it be possible.
Reply
Princy
17421 posts
Member
since:
Mar 2007
Link to this postPosted 12 May 2014 in reply to Muhammad
Hi Muhammad,
You can have a GridTemplateColumn to achieve your requirement. To create
a template dynamically, you must define a custom class that implements
the ITemplate interface. Then you can assign an instance of this class to the
ItemTemplate or EditTemplate property of the GridTemplateColumn object.
Below is a sample code that shows how to have a DropDown in edit and label
in view mode, similarly you can create other columns.
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
2. 9/2/2015 Dynamically define Rad Grid using Code Behind in C# Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/dynamicallydefineradgridusingcodebehindinc 2/8
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
C#:
RadGrid RadGrid1;
protected void Page_Init(object sender, EventArgs e)
{
RadGrid1 = new RadGrid();
RadGrid1.DataSourceID = "SqlDataSource1";
RadGrid1.MasterTableView.DataKeyNames = new string[] {
"OrderID" };
RadGrid1.AllowPaging = true;
RadGrid1.AutoGenerateColumns = false;
RadGrid1.AutoGenerateEditColumn = true;
RadGrid1.ItemDataBound += new
GridItemEventHandler(RadGrid1_ItemDataBound);
GridBoundColumn boundColumn1;
boundColumn1 = new GridBoundColumn();
boundColumn1.DataField = "OrderID";
boundColumn1.HeaderText = "OrderID";
boundColumn1.UniqueName = "OrderID";
RadGrid1.MasterTableView.Columns.Add(boundColumn1);
GridBoundColumn boundColumn2;
boundColumn2 = new GridBoundColumn();
boundColumn2.DataField = "ShipCity";
boundColumn2.HeaderText = "ShipCity";
boundColumn2.UniqueName = "ShipCity";
RadGrid1.MasterTableView.Columns.Add(boundColumn2);
string templateColumnName = "employeeid";
GridTemplateColumn templateColumn = new
GridTemplateColumn();
templateColumn.ItemTemplate = new
MyTemplate(templateColumnName);
templateColumn.EditItemTemplate = new MyEditTemplate();
templateColumn.HeaderText = templateColumnName;
templateColumn.DataField = "EmployeeID";
RadGrid1.MasterTableView.Columns.Add(templateColumn);
PlaceHolder1.Controls.Add(RadGrid1);
}
public class MyTemplate : ITemplate
{
private string colname;
protected Label lControl;
public MyTemplate(string cName)
{
colname = cName;
}
3. 9/2/2015 Dynamically define Rad Grid using Code Behind in C# Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/dynamicallydefineradgridusingcodebehindinc 3/8
}
public void InstantiateIn(System.Web.UI.Control container)
{
lControl = new Label();
lControl.ID = "lControl";
lControl.DataBinding += new
EventHandler(lControl_DataBinding);
container.Controls.Add(lControl);
}
public void lControl_DataBinding(object sender, EventArgs
e)
{
Label l = (Label)sender;
GridDataItem container =
(GridDataItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)
[colname].ToString() + "<br />";
}
}
public class MyEditTemplate : IBindableTemplate
{
public void InstantiateIn(Control container)
{
GridEditFormItem item = ((GridEditFormItem)
(container.NamingContainer));
DropDownList drop = new DropDownList();
drop.ID = "dropdownlist1";
container.Controls.Add(drop);
}
public System.Collections.Specialized.IOrderedDictionary
ExtractValues(System.Web.UI.Control container)
{
OrderedDictionary od = new OrderedDictionary();
od.Add("OrderID", ((DropDownList)
(((GridEditFormItem)
(container)).FindControl("dropdownlist1"))).DataValueField);
return od;
}
}
protected void RadGrid1_ItemDataBound(object sender,
Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
{
GridEditFormItem editItem =
(GridEditFormItem)e.Item;
4. 9/2/2015 Dynamically define Rad Grid using Code Behind in C# Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/dynamicallydefineradgridusingcodebehindinc 4/8
DropDownList ddl =
(DropDownList)editItem.FindControl("dropdownlist1");
ddl.DataSourceID = "SqlDataSource1";
ddl.DataTextField = "Employeeid";
ddl.DataValueField = "Employeeid";
ddl.SelectedIndex = editItem.ItemIndex;
}
}
Thanks,
Princy
Reply
Muhamma
d
4 posts
Member
since:
Dec 2013
Link to this postPosted 13 May 2014
Thanks Princy for Reply its work fine i want to add one thing more into it
when user click's on edit button Dropdown list Populate's,
but Selected value is not defined as previous selected value lets suppose
EmployeeID = 5 when user Click on Edit Button Dropdown List Show's but
Selected Value of Dropdown is 1 i want to dropdown to Select previos value
as previously defined.
Reply
Muhamma
d
4 posts
Member
since:
Dec 2013
Link to this postPosted 13 May 2014
and when i click add or edit button it does not fires any event how to make it
possible
Reply
Princy Link to this postPosted 13 May 2014 in reply to Muhammad