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.




1 comment:

Anonymous said...

Hi,
this is nice solution
but how to do binding of this ?