Results 1 to 4 of 4

Thread: DataGrid Header Text Set Dynamically, but Not Being Retrieved

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    8

    DataGrid Header Text Set Dynamically, but Not Being Retrieved

    I have a DataGrid defined with columns such as:
    Code:
    <asp:BoundColumn HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%" DataField="MONTH_PREVIOUS_YEAR" HeaderText="Month Hours (Previous Year)" ItemStyle-HorizontalAlign="Right"></asp:BoundColumn>
    At a certain point in my program, I successfully re-set some of the Header titles programmatically:
    Code:
        Private Sub dgAccountability_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAccountability.ItemDataBound
            If e.Item.ItemType = ListItemType.Header Then
                Dim previousYear As Integer = (Integer.Parse(year) - 1)
                e.Item.Cells(1).Text = "YTD Hours " & previousYear.ToString
                e.Item.Cells(2).Text = "YTD Hours " & year
                e.Item.Cells(4).Text = month & " Hours " & previousYear.ToString
                e.Item.Cells(5).Text = month & " Hours " & year
                Dim previousMonth As String
                If ddlMonths.SelectedIndex = 0 Then
                    previousMonth = "December"
                    e.Item.Cells(7).Text = previousMonth & " Hours " & previousYear
                Else
                    previousMonth = ddlMonths.Items(ddlMonths.SelectedIndex - 1).Value
                    e.Item.Cells(7).Text = previousMonth & " Hours " & year
                End If
            End If
        End Sub
    Here's the snare I've hit. If a user chooses to click a button send the data to an Excel spreadsheet, I would like to use the dynamically built header titles. For some reason, Row 0 contains my first row of data, not my titles. Therefore, I've worked around it by using this:
    Code:
    tbl.Rows(0).Cells(c).Text = dg.Columns(c).HeaderText.ToString
    Unfortunately, that is picking up the original titles, not the dynamically set ones. Any thoughts or ideas?

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: DataGrid Header Text Set Dynamically, but Not Being Retrieved

    Hi,

    I believe this is ASP.NET version 1.x since your using the DataGrid control. I tried using the GridView control in ASP.NET 2.0 - 4.0.
    I can set the header correctly and retrieve the dynamically changed header text during postback/button click.

    See code below:

    Code:
    <asp:GridView ID="grdAccountability1" runat="server" BackColor="White"  AutoGenerateColumns="false"
            BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            onrowdatabound="grdAccountability1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Address" HeaderText="Address" />
                <asp:BoundField DataField="Contact" HeaderText="Contact Information" />
            </Columns>
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <RowStyle BackColor="White" ForeColor="#003399" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <SortedAscendingCellStyle BackColor="#EDF6F6" />
            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
            <SortedDescendingCellStyle BackColor="#D6DFDF" />
            <SortedDescendingHeaderStyle BackColor="#002876" />
        </asp:GridView>
        <br />
        <asp:Label ID="lblDisplay" runat="server" Text=""></asp:Label>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
    Code:
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            BindToGrid()
        End If
    End Sub
    
    Private Sub BindToGrid()
        Dim table As New DataTable()
        table.Columns.Add("Name", GetType(String))
        table.Columns.Add("Address", GetType(String))
        table.Columns.Add("Contact", GetType(String))
        table.Rows.Add("Greg", "Cebu", "greg@mail.com")
        table.Rows.Add("John", "Manila", "john@mail.com")
        table.Rows.Add("Mike", "Bohol", "mike@mail.com")
        grdAccountability1.DataSource = table
        grdAccountability1.DataBind()
    End Sub
    
    Protected Sub grdAccountability1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).Text = "Complete Name"
        End If
    End Sub
    
    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
        lblDisplay.Text = String.Format("First Column Header Cell Value: {0}", grdAccountability1.HeaderRow.Cells(0).Text)
    End Sub
    Suggestion, why not use the GridView control instead?

    Greg
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    8

    Re: DataGrid Header Text Set Dynamically, but Not Being Retrieved

    Thanks, Greg. I'll check into the GridView control.
    I was originally a Java programmer that took a new position last September. I've done a lot of self-teaching of VB, VB.Net, and ASP.Net the last few months. My current project is a total overhaul of an existing project, and that existing one used DataGrids and ListViews, depending on the report being produced. I decided to make them all consistent and went with the DataGrid. I confess that I never considered a third choice altogether. I appreciate your suggestion.

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: DataGrid Header Text Set Dynamically, but Not Being Retrieved

    Your welcome...
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width