Programmatically set template column column header value

Discussion in 'ASP .Net Datagrid Control' started by jdhavo@yahoo.com, Aug 18, 2006.

  1. Guest

    Has anyone ever set the header value for a template column in a
    datagrid programmatically?
    , Aug 18, 2006
    #1
    1. Advertising

  2. All you have to do is catch the ItemCreated event for the header row and
    then check the text of the column header. Here's an example.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]


    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs)
    If Not IsPostBack Then
    datagrid1.DataSource = CreateDataSource()
    datagrid1.DataBind()
    End If
    End Sub


    Function CreateDataSource() As Data.DataTable
    Dim dt As New Data.DataTable
    Dim dr As Data.DataRow
    dt.Columns.Add(New Data.DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New Data.DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New Data.DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New Data.DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 5
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function

    Protected Sub datagrid1_ItemCreated(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs)
    If e.Item.ItemType = ListItemType.Header Then
    e.Item.Cells(0).Text = "Set programmatically"
    End If
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Set datagrid header programmatically</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:datagrid runat="server" id="datagrid1"
    onitemcreated="datagrid1_ItemCreated" autogeneratecolumns="False">
    <columns>
    <asp:templatecolumn>
    <itemtemplate>
    <asp:label runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.StringValue") %>'></asp:label>
    </itemtemplate>
    </asp:templatecolumn>
    </columns>

    </asp:datagrid><br />
    </div>
    </form>
    </body>
    </html>

    <> wrote in message
    news:...
    > Has anyone ever set the header value for a template column in a
    > datagrid programmatically?
    >
    Ken Cox [Microsoft MVP], Aug 19, 2006
    #2
    1. Advertising

  3. jyoti Guest

    wrote:
    > Has anyone ever set the header value for a template column in a
    > datagrid programmatically?



    yes,
    by using html coding u can do that
    jyoti, Aug 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Mason
    Replies:
    0
    Views:
    845
    John Mason
    Aug 20, 2006
  2. mlt
    Replies:
    2
    Views:
    812
    Jean-Marc Bourguet
    Jan 31, 2009
  3. hansiman

    Image in header column (not replacing column header text)

    hansiman, Feb 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    291
    hansiman
    Feb 7, 2004
  4. Esperanza

    Template column base on another column value

    Esperanza, Feb 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    165
    Elton Wang
    Feb 21, 2005
  5. datagrid having row header and column header

    , Jul 13, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    164
Loading...

Share This Page