ASP.NET Gridview Sort by Header removed when Header Text Changed at Runtime

I am using the following code to Change the Header Text of a column based on a condtion stored in a session variable.  When I do this the ability to Click on the Header in the GridView to sort is lost.  How do I get that to work again after the header text is changed?

Here is code in the Gridview Row_Created Event:

protected void gvReport_RowCreated(object sender, GridViewRowEventArgs e)
    {
        // *** change the "UNorRS" field based on CropType Selected ***
        GridViewRow gvr = e.Row;
        string cropType = string.Empty;

        try
          {
            cropType = Session["CropType"].ToString();
          }
          catch (Exception)
          {}
       

        if (gvr.RowType == DataControlRowType.Header)
        {
           
            if (cropType == "2")
            {
                gvr.Cells[8].Text = "Nitrogen"; // IF CORN
            }
            if (cropType == "3")
            {
                gvr.Cells[8].Text = "Row Space"; // IF SOYBEAN
            }

        }

    }

Here is the Souce Code on the aspx page for the Templated Field in question (ignor bound fields)


<asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataSourceID="dsrcRESULTS" ForeColor="#333333" GridLines="None" Visible="False" AllowSorting="True" OnRowCreated="gvReport_RowCreated">
            <RowStyle BackColor="#E3EAEB" />
            <Columns>
                <asp:BoundField DataField="Source" HeaderText="Source" SortExpression="Source" />
                <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
                <asp:BoundField DataField="SoilType" HeaderText="SoilType"
                <asp:TemplateField HeaderText="UNorRS" SortExpression="UNorRS">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UNorRS") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("UNorRS") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
jholmes0724Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pendemCommented:
Write your code in RowDataBound event instead of RowCreated.

<asp:GridView .... OnRowDataBound = "gvReport_RowDataBound">


Protected Sub gvReport_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
<Your code>
End Sub
0
jholmes0724Author Commented:
pendem,
That did not work, still the same issue.  Made sense though but for some reason it loses it's Sorting capability when I do this. Not sure why.   Any other ideas on this?
0
pendemCommented:
In the RowDataBountEvent write the code as below:

 


if (gvr.RowType == DataControlRowType.Header)
        {
           
            LinkButton lbtn = gvr.Cells[8].Controls[0];
            if (cropType == "2")
            {
                lbtn.Text = "Nitrogen"; // IF CORN
            }
            if (cropType == "3")
            {
                lbtn.Text = "Row Space"; // IF SOYBEAN
            }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jholmes0724Author Commented:
Awesome, that worked.   The only thing I had to do different on your code was to Cast the gvr.cells to Linkbutton.  Changed the line as follows:

LinkButton lbtn = (LinkButton)gvr.Cells[8].Controls[0];

Once I did that and it worked perfectly.. Thanks so much.

0
jholmes0724Author Commented:
Very quick response and follow-up response.  Excellent  Thanks so much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.