Reputation: 2941
I am adding columns to RadGrid from code behind. In NeedDataSource event, I am binding a DataTable(with 10 columns) to the radgrid.
Everything's working well till here. But I would like to have text boxes in 2 columns(on load itself, not just in edit mode).
<telerik:RadGrid ID="RadGrid1" runat="server" ShowHeader="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender"
AutoGenerateColumns="true" >
<MasterTableView>
</MasterTableView>
</telerik:RadGrid>
If done declarative, the column definition shall be like this. But I want it accomplished from code behind.
<telerik:GridTemplateColumn HeaderText="Qty">
<ItemTemplate>
<input id="<%# this.GetUniqueId("Qty", Container.DataItem)%>" name="<%# this.GetUniqueId("Qty", Container.DataItem)%>" type="text" value="<%# Eval("Quantity")%>" size="2" maxlength="3" />
</ItemTemplate>
</telerik:GridTemplateColumn>
Upvotes: 0
Views: 12090
Reputation: 31
Use This.
GridTemplateColumn tempCol;
for (int i = 0; i < obj.Count; i++)
{
tempCol = new GridTemplateColumn();
this.gvwRejection.MasterTableView.Columns.Add(tempCol);
tempCol.ItemTemplate = new DynamicTemplateCoulmn"txtCategoryQty"+ , "numericTextBox");
tempCol.HeaderText = objRejectionCategoryMasterObject[i].CategoryName.Trim();
tempCol.UniqueName = "CategoryQty" + i;
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
RejCategoryIDs[i] = objRejectionCategoryMasterObject[i].RejCategoryID;
}
tempCol = new GridTemplateColumn();
this.gvwRejection.MasterTableView.Columns.Add(tempCol);
tempCol.ItemTemplate = new DynamicTemplateCoulmn("txtTotal", "numericTextBoxReadOnly");
tempCol.HeaderText = "Total";
tempCol.UniqueName = "Total";
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
Upvotes: 0
Reputation: 407
Must you use a template column? If you can directly bind your columns to a datasource field, use the GridNumericColumn. This can be dynamically added from the code behind:
GridNumericColumn numColumn = new GridNumericColumn();
numColumn.UniqueName = "ColumnId";
numColumn.MaxLength = 20;
numColumn.HeaderText = "My Numeric Column";
numColumn.DataField = "Qty";
numColumn.DataFormatString =
myGrid.MasterTableView.Columns.Add(numColumn);
Upvotes: 1
Reputation: 20693
Create TemplateColumn like any other column type and set template object to ItemTemplate (and you can do same for HeaderTemplate and FooterTemplate). But you have to define custom template class witch will implement ITemplate
.
You can find an example here :
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section4
Upvotes: 2