Gridview fail to change header text after sorted

WONG Tony 161 Reputation points

the gridview is created dynamically at runtime
i tried to change header text at runtime.
it is ok at first load up
but it change back to sql text field name after sorted.

DirectCast(GV_Activity.HeaderRow.Cells(44).Controls(0), LinkButton).Text = "GV loaded"

Gridview load - not ispostback - OK

All other events failed to change
Gridview load ispostback
Gridview sorted
Gridview disposed
Gridview unload

When i check the text at runtime, it show the changed text but not show in web page

Sorting is OK in all cases. Thank you for your help

A set of technologies in the .NET Framework for building web applications and XML web services.
3,411 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 28,836 Reputation points Microsoft Vendor

    Hi @WONG Tony ,
    Maybe you can write it another way. For details, you can refer to the code below.

     <asp:GridView ID="GridView1" runat="server"   DataKeyNames="ID"  AllowSorting="true"   OnSorting="gridView_Sorting" OnSorted="GridView1_Sorted" />  

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
            Dim constr As String = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString  
            Using con As SqlConnection = New SqlConnection(constr)  
                Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Test ")  
                    Using sda As SqlDataAdapter = New SqlDataAdapter()  
                        cmd.Connection = con  
                        sda.SelectCommand = cmd  
                        Using dt As DataTable = New DataTable()  
                            ViewState("dirState") = dt  
                            GridView1.DataSource = dt  
                        End Using  
                    End Using  
                End Using  
            End Using  
        End Sub  
        Private Function ConvertSortDirectionToSql(ByVal column As String) As String  
            Dim sortDirection As String = "ASC"  
            Dim sortExpression As String = TryCast(ViewState("SortExpression"), String)  
            If sortExpression IsNot Nothing Then  
                If sortExpression = column Then  
                    Dim lastDirection As String = TryCast(ViewState("SortDirection"), String)  
                    If (lastDirection IsNot Nothing) AndAlso (lastDirection = "ASC") Then  
                        sortDirection = "DESC"  
                    End If  
                End If  
            End If  
            ViewState("SortDirection") = sortDirection  
            ViewState("SortExpression") = column  
            Return sortDirection  
        End Function  
        Protected Sub gridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)  
            Dim dataTable As DataTable = TryCast(GridView1.DataSource, DataTable)  
            If dataTable IsNot Nothing Then  
                Dim dataView As DataView = New DataView(dataTable)  
                dataView.Sort = e.SortExpression & " " & ConvertSortDirectionToSql(e.SortExpression)  
                GridView1.DataSource = dataView  
            End If  
        End Sub  
        Protected Sub GridView1_Sorted(ByVal sender As Object, ByVal e As EventArgs)  
            GridView1.HeaderRow.Cells(0).Text = "GV sorted"  
        End Sub  

    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. WONG Tony 161 Reputation points

    Hi LanHuang

    Thank you for your help

    Finally, by trial and error

    it is added to gridview rowdatabound event, footer row type even there is no footer in gridview

    it can update the header now