Gridview fail to change header text after sorted

WONG Tony 161 Reputation points
2022-11-29T04:52:17.51+00:00

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
RowCreated
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

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

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2022-11-29T09:15:14.417+00:00

    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()  
                            sda.Fill(dt)  
                            ViewState("dirState") = dt  
                            GridView1.DataSource = dt  
                            GridView1.DataBind()  
                        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  
                GridView1.DataBind()  
            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  
    

    265192-16.gif
    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
    2022-12-01T07:49:07.35+00:00

    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