Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / August 2006

Tip: Looking for answers? Try searching our database.

Programmatically set template column column header value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jdhavo@yahoo.com - 18 Aug 2006 19:16 GMT
Has anyone ever set the header value for a template column in a
datagrid programmatically?
Ken Cox [Microsoft MVP] - 19 Aug 2006 03:02 GMT
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>

> Has anyone ever set the header value for a template column in a
> datagrid programmatically?
jyoti - 19 Aug 2006 05:17 GMT
> Has anyone ever set the header value for a template column in a
> datagrid programmatically?

yes,
by using html coding u can do that

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.