Is it possible to add template columns after Page_Init?
I have dynamic columns in a radgrid that is nested inside another radgrid that can only be calculated after the parent radgrid is bound.
This was working fine when I was binding the parent in Page_Init, but now I am binding it with the NeedDataSource event and the template columns no longer render, since Page_Init has already fired.
Is there anything I can do after Page_Init to render the columns added?
I have dynamic columns in a radgrid that is nested inside another radgrid that can only be calculated after the parent radgrid is bound.
This was working fine when I was binding the parent in Page_Init, but now I am binding it with the NeedDataSource event and the template columns no longer render, since Page_Init has already fired.
Is there anything I can do after Page_Init to render the columns added?
4 Answers, 1 is accepted
0

Princy
Top achievements
Rank 2
answered on 24 Mar 2014, 04:29 AM
Hi Eric,
When creating template columns programmatically, the grid must be generated completely in the code-behind using the Page_Init event. It must be added in the Page_Init event handler, so that the template controls can be added to the ViewState. Please take a look at this article on Creating TemplateColumns.
Thanks,
Princy
When creating template columns programmatically, the grid must be generated completely in the code-behind using the Page_Init event. It must be added in the Page_Init event handler, so that the template controls can be added to the ViewState. Please take a look at this article on Creating TemplateColumns.
Thanks,
Princy
0

Eric
Top achievements
Rank 1
answered on 24 Mar 2014, 12:58 PM
When I do everything in Page_init paging doesn't work properly, so I move everything to NeedDatasource event for paging to work but then programmatically generated columns do not show up.
Is there a way to use NeedDatasource event so paging works while also creating columns programatically in page_init or do I have to do custom paging?
Is there a way to use NeedDatasource event so paging works while also creating columns programatically in page_init or do I have to do custom paging?
0
Accepted

Princy
Top achievements
Rank 2
answered on 26 Mar 2014, 05:57 AM
Hi Eric,
I was not able to replicate any issue at my end. Here is a sample code snippet which works as expected. Provide your full code snippet for further help.
ASPX:
C#:
Thanks,
Princy
I was not able to replicate any issue at my end. Here is a sample code snippet which works as expected. Provide your full code snippet for further help.
ASPX:
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
></
asp:PlaceHolder
>
C#:
RadGrid RadGrid1;
protected
void
Page_Init(
object
source, System.EventArgs e)
{
RadGrid1 =
new
RadGrid();
RadGrid1.MasterTableView.DataKeyNames =
new
string
[] {
"OrderID"
};
RadGrid1.Skin =
"Default"
;
RadGrid1.Width = Unit.Percentage(100);
RadGrid1.PageSize = 15;
RadGrid1.AllowPaging =
true
;
RadGrid1.AutoGenerateColumns =
false
;
RadGrid1.AllowSorting =
true
;
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
GridBoundColumn boundColumn;
boundColumn =
new
GridBoundColumn();
boundColumn.DataField =
"OrderID"
;
boundColumn.HeaderText =
"OrderID"
;
boundColumn.SortExpression =
"OrderID"
;
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new
GridBoundColumn();
boundColumn.DataField =
"ShipCountry"
;
boundColumn.HeaderText =
"ShipCountry"
;
boundColumn.SortExpression =
"ShipCountry"
;
RadGrid1.MasterTableView.Columns.Add(boundColumn);
string
templateColumnName =
"CustomerID"
;
GridTemplateColumn templateColumn =
new
GridTemplateColumn();
templateColumn.ItemTemplate =
new
MyTemplate(templateColumnName);
templateColumn.HeaderText = templateColumnName;
templateColumn.SortExpression =
"CustomerID"
;
RadGrid1.MasterTableView.Columns.Add(templateColumn);
this
.PlaceHolder1.Controls.Add(RadGrid1);
}
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = GetDataTable(
"SELECT * FROM Orders"
);
}
public
DataTable GetDataTable(
string
query)
{
String ConnString = ConfigurationManager.ConnectionStrings[
"Northwind_newConnectionString3"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(ConnString);
SqlDataAdapter adapter =
new
SqlDataAdapter();
adapter.SelectCommand =
new
SqlCommand(query, conn);
DataTable myDataTable =
new
DataTable();
conn.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
conn.Close();
}
return
myDataTable;
}
private
class
MyTemplate : ITemplate
{
protected
LiteralControl lControl;
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);
container.Controls.Add(lControl);
}
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,
Princy
0

Eric
Top achievements
Rank 1
answered on 26 Mar 2014, 01:19 PM
Thanks that works