how i get the column header text when clicked in gridview in asp.net

HOUSSEM MAHJOUBI 286 Reputation points
2020-12-31T09:27:40.113+00:00

Hi members

how i get the column header text when clicked in gridview

please help

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

Accepted answer
  1. Yijing Sun-MSFT 7,076 Reputation points
    2021-01-01T08:19:16.403+00:00

    Hi @HOUSSEM MAHJOUBI ,
    Accroding to your description,how do you click in the gridview using button? And what's need you do to get the column header text?

    I have created a demo that you could click on the column header and get the text using rowdatabound and javascript click event.Just like this:

    <asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false" OnRowDataBound="gv1_RowDataBound">  
        <Columns>  
            <asp:TemplateField HeaderText="Id">  
                <ItemTemplate>  
                    <asp:Label runat="server"  ID="lbid" Text='<%# E val("Id") >'></asp:Label>  
                </ItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="num">  
               <ItemTemplate>  
                 <asp:Label runat="server" ID="lbid" Text='<%# E val("num") %>'></asp:Label>  
               </ItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="Total">  
                <ItemTemplate>  
                  <asp:Label runat="server" ID="lbid" Text='<%# E val("num") %>'></asp:Label>  
                </ItemTemplate>  
            </asp:TemplateField>  
        </Columns>  
        </asp:GridView>  
    
     function GetHeaderText(txt) {  
          a lert(txt);  
                }  
    

    Code-behind:

     protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)  
            {  
             if (e.Row.RowType == DataControlRowType.Header)  
                {  
                     foreach (DataControlFieldCell cell in e.Row.Cells)  
                      {  
                           cell.Attributes.Add("onclick", "[javascript]: GetHeaderText('" + cell.Text + "')");  //You need to remove [] around the word 'javascript'   
                      }  
                }  
            }  
                    
    Result:  
    

    7kX8w.gif

    Edited:

      Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)  
            If e.Row.RowType = DataControlRowType.Header Then  
          
                For Each cell As DataControlFieldCell In e.Row.Cells  
                    cell.Attributes.Add("onclick", "[javascript]:GetHeaderText('" & cell.Text & "')")  
                Next  
            End If  
        End Sub  
        <asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false" OnRowDataBound="gv1_RowDataBound"></asp:GridView>  
    

    Create another gridview and show the data
    Since I don't know the relationship of gridviews' data,but I suggest you could create gridview in jquery and use ajax to fill data when you click the gridview column.
    Just like this:

    Code-behind:  
       cell.Attributes.Add("onclick", "test()");  
    
     <System.Web.Services.WebMethod>  
    Public Shared Function test2() As String  
    End Function  
    

    Jquery:

    $(function () {  
                test();  
            });  
            function test() {  
                $.a jax({  
                    type: "POST",  
                    url: "Default.aspx/test2",  // call sql query in code behind   
                    contentType: "application/json; charset=utf-8",  
                    dataType: "json",  
                    success: function(){  
                    $("#gvProduct").append()// create gridview and fill data  
                     }  
                    failure: function (response) {  
                        a lert(response.d);  
                    },    
                });  
            }  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.

    Best regards,
    Yijing Sun

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. HOUSSEM MAHJOUBI 286 Reputation points
    2021-01-04T08:50:34.627+00:00

    i generate the gridview rows from a datatable this is my code

    Protected Sub mouvement(ByVal table As DataTable)
            Dim tab As New DataTable
            Dim annee As String = Now.Year
            MsgBox(annee)
            tab.Columns.Add("Article")
            tab.Columns.Add("Désignation")
            tab.Columns.Add(annee - 3)
            tab.Columns.Add(annee - 2)
            tab.Columns.Add(annee - 1)
            tab.Columns.Add(annee)
            Dim Total1 As Integer
            Dim Total2 As Integer
            Dim Total3 As Integer
            Dim Total4 As Integer
                 For i = 0 To table.Rows.Count - 1
                       If cn.State = ConnectionState.Open Then
                    cn.Close()
                End If
                    cn.Open()
                     Dim cmd2 As New SqlCommand("select SM03,SM02,SM01,SM00 from mvt4 where AR_ref='" & table.Rows(i).Item(0) & "'", cn)
                Dim sqlreader As SqlDataReader = (cmd2.ExecuteReader)
                    sqlreader.Read()
                If sqlreader.HasRows Then
                        tab.Rows.Add(table.Rows(i).Item(0), table.Rows(i).Item(1), sqlreader(0), sqlreader(1), sqlreader(2), sqlreader(3))
                    Total1 = Total1 + sqlreader(0)
                    Total2 = Total2 + sqlreader(1)
                    Total3 = Total3 + sqlreader(2)
                    Total4 = Total4 + sqlreader(3)
                End If
                Next
    
            tab.Rows.Add("", "", "", "", "", "")
            tab.Rows.Add("", "", "", "", "", "")
            tab.Rows.Add("TOTAL", "", Total1, Total2, Total3, Total4)
                GridView5.DataSource = tab
            GridView5.DataBind()
          End Sub
    

    i get the header text in popup cause of javascript alert but i want it in variable in vb.net code
    because i want use it in sql query after
    please help


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.