Tuesday, August 07, 2007

Dynamic Template Columns in the ASP.NET 2.0 GridView Control: Part 2

Sorry for the delay. It's a bit late to publish the continuation of my earlier post. As I mentioned previously you can add a dynamic column to gridview. However there is another way of doing it. It depends on your requirement. The 2 nd method is much easier though.

You have to create a Usercontrol. Let's say you need to have a Text box in your grid. Then just add a text box to your Usercontrol and in HTML view you can edit it as shown below... (Other than a Textbox I have add a CompareValidator to check the data type of the Text box.)


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserCont.ascx.cs" Inherits="TestApp_MyUserCont" %>
' Width="90px">
ID="CompareValidator1" runat="server" Display="Dynamic" Type="Currency" Operator="DataTypeCheck"
ErrorMessage="*" ControlToValidate="txtQty">




In the code Text='<%# Bind("Quantity") %>' take care of Binding data to the Text property of the Textbox. "Quantity" is the Column name of my data table (gridview data source).

Next is to create your Dynamic column as follows...


TemplateField tmpQty = new TemplateField();
tmpQty.HeaderText = "
Quantity";
tmpQty.ItemTemplate = LoadTemplate("../
TestApp/MyUserCont.ascx");
tmpQty.Visible = true;
gvTransaction.Columns.Add(tmpQty);

Hope this will make things easier than the earlier method. But there are some limitations. So as I mentioned earlier you have to identify which method is suitable according to your requirement.




Friday, August 03, 2007

Dynamic Template Columns in the ASP.NET 2.0 GridView Control: Part 1

Recently I was involved in a project where we needed to present the results of a database query as part of an ASP.NET application. It was such that the user can select grid columns and the type of the columns also Differentiating depending on the requirement. It may be a button, column, editable column (text box,check box,etc...) It was not known until runtime how many columns has to display, which columns has to display or which SQL query is needed to satisfy the search criteria. So I had to use dynamics template coloumns for the editable coloumns.

A GridView template is a class that implements the ITemplate interface. It defines the controls that will be displayed on the GridView in a column.I thought it's worthwhile to share it here.


// Class for define Template, this class will added to your app_Code folder.
public class SampleTemplate : ITemplate
{
private string _DataField;

public
SampleTemplate (string DataField)
{
this._DataField = DataField;
}

public void InstantiateIn(Control container)
{
TextBox txtItemVal = new TextBox();
txtItemVal.DataBinding += new EventHandler(this.OnDataBinding);
txtItemVal .ID = "txtItemVal";
container.Controls.Add(
txtItemVal);
}

public void OnDataBinding(object sender, EventArgs e)
{
TextBox TXT = (TextBox)sender;
GridViewRow container = (GridViewRow)TXT.NamingContainer;
string id = ((DataRowView)container.DataItem)[_DataField].ToString();
TXT.Text= id;

}
}



Adding template to your grid.code behind file.In my case i am going through a for loop with in the user datacolumns datatable and adding coloumns to gridview.

TemplateField tmpItem= new TemplateField();

// you have to specify column name as "datafield" here.as for the gridview data source.lets say you //have Employee datatable.and you want to show Empage in this template coloumn.then you should //use empAge as your dataField

SampleTemplate TEMP1 = new SampleTemplate ("Empage");
tmpUnits.HeaderText = "Employee Age"
tmpUnits.ItemTemplate = TEMP1;
gvTransaction.Columns.Add(
tmpItem);

Thats it.Hope this will help you one day.And there's a another way also.will tell you latter. ;)