How to add TemplateField programmatically in C#

How to add TemplateField programmatically in C#

To add a TemplateField programmatically in C#, you can create a new instance of the TemplateField class and set its properties as needed. Here's an example:

TemplateField myField = new TemplateField(); myField.HeaderText = "My Header"; myField.ItemTemplate = new MyItemTemplate(); 

In this example, we're creating a new instance of TemplateField and setting its HeaderText property to "My Header". We're also creating a custom item template by defining a class MyItemTemplate that implements the ITemplate interface. The ItemTemplate property is then set to an instance of this custom item template.

Here's an example of a simple custom item template that just displays a string value:

public class MyItemTemplate : ITemplate { public void InstantiateIn(Control container) { LiteralControl literal = new LiteralControl(); literal.DataBinding += new EventHandler(literal_DataBinding); container.Controls.Add(literal); } void literal_DataBinding(object sender, EventArgs e) { LiteralControl literal = (LiteralControl)sender; GridViewRow container = (GridViewRow)literal.NamingContainer; literal.Text = DataBinder.Eval(container.DataItem, "MyField").ToString(); } } 

In this example, we're defining a class MyItemTemplate that implements the ITemplate interface. The InstantiateIn method creates a new LiteralControl and sets up an event handler for the DataBinding event. The event handler sets the Text property of the LiteralControl to the value of a field named "MyField" in the data item for the current GridViewRow.

To add the TemplateField to a GridView, you can use the Columns collection:

myGridView.Columns.Add(myField); 

In this example, we're adding the myField TemplateField to a GridView named myGridView by adding it to the Columns collection.

Examples

  1. "C# add TemplateField to GridView programmatically"

    // Code: TemplateField templateField = new TemplateField(); gridView.Columns.Add(templateField); 

    Description: Demonstrates adding a TemplateField to a GridView programmatically.

  2. "C# GridView TemplateField dynamically set HeaderText"

    // Code: templateField.HeaderText = "Custom Header Text"; 

    Description: Sets a custom header text for the dynamically added TemplateField in a GridView.

  3. "C# GridView TemplateField set ItemTemplate"

    // Code: templateField.ItemTemplate = new MyCustomTemplate(); 

    Description: Assigns a custom item template (MyCustomTemplate) to the TemplateField in a GridView.

  4. "C# GridView TemplateField set EditItemTemplate"

    // Code: templateField.EditItemTemplate = new MyEditTemplate(); 

    Description: Assigns a custom edit item template (MyEditTemplate) to the TemplateField in a GridView.

  5. "C# GridView TemplateField set FooterTemplate"

    // Code: templateField.FooterTemplate = new MyFooterTemplate(); 

    Description: Assigns a custom footer template (MyFooterTemplate) to the TemplateField in a GridView.

  6. "C# GridView TemplateField add controls programmatically"

    // Code: templateField.ItemTemplate = new MyCustomTemplate(); 

    Description: Demonstrates how to programmatically add controls to the item template (MyCustomTemplate) of the TemplateField.

  7. "C# GridView TemplateField bind data programmatically"

    // Code: templateField.ItemTemplate = new MyDataBoundTemplate(); 

    Description: Shows how to bind data programmatically in the item template (MyDataBoundTemplate) of the TemplateField.

  8. "C# GridView TemplateField set SortExpression"

    // Code: templateField.SortExpression = "FieldName"; 

    Description: Sets the sort expression for the TemplateField to enable sorting in a GridView.

  9. "C# GridView TemplateField set Control ID dynamically"

    // Code: templateField.ItemTemplate = new MyCustomTemplate(); templateField.ItemTemplate.InstantiateIn(new TableCell { ID = "MyCellID" }); 

    Description: Dynamically sets the control ID for controls in the item template (MyCustomTemplate) of the TemplateField.

  10. "C# GridView TemplateField set ItemStyle dynamically"

    // Code: templateField.ItemStyle.BackColor = System.Drawing.Color.LightBlue; 

    Description: Dynamically sets the ItemStyle properties, such as the background color, for the TemplateField in a GridView.


More Tags

centering nuget-package uistackview objective-c 3des devicetoken amazon-athena malloc javafx-2 inline-assembly

More C# Questions

More Physical chemistry Calculators

More Transportation Calculators

More Retirement Calculators

More Housing Building Calculators