简体   繁体   中英

get the selected column header text in gridview c#

In my asp.net application i had a gridview with two link buttons as two columns. and in gridview had some rows , now my question is how to get the column header text when a column value is selected.

like i had five columns (A,B,C,D and E) in gridview in which (B and E) are link buttons,, and the gridview had some rows binded. now if i select a value(X) in column Say "B" i want to get the column header text "B" in code behind and in similar to column E... have to differentiate the selected column header text...... Here is the gridview

 <asp:GridView Width="100%" ID="grdReport" OnRowCommand="grdReport_RowCommand" runat="server" 
                    CssClass="table table-striped table-bordered" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="LetterID" HeaderText="Letter ID" ReadOnly="True" SortExpression="LetterID" Visible="false" />
                        <asp:TemplateField HeaderText="SNo" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100">
                            <ItemTemplate>
                                <asp:Label ID="lblSNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Ref No" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkRefNo" runat="server" CommandName="Select" CommandArgument='<%# Bind("LetterID") %>' Text='<%# Eval("RefNo") %>'  />
                            </ItemTemplate>
                        </asp:TemplateField>

                        <asp:BoundField DataField="LetterDate" ItemStyle-CssClass="text-center" ItemStyle-Width="100" DataFormatString="{0:dd/MMM/yyyy}" HeaderText="Date" ReadOnly="True" SortExpression="LetterDate" />

                        <asp:TemplateField HeaderText="From Company" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100">
                            <ItemTemplate>
                                <asp:Label ID="lnkfrmCompany" Text='<%# Bind("FromCompany") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="To Company" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100">
                            <ItemTemplate>
                                <asp:Label ID="lnkToCompany" Text='<%# Bind("ToCompany") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Subject" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkSubject" CommandName="Select" CommandArgument='<%# Bind("LetterID") %>' Text='<%# Bind("Subject") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle HorizontalAlign="Right" CssClass="GridPager" />

                    <EmptyDataTemplate>
                        <center><span style="color: red;"><i class="fa fa-users fa-2x"></i>  No Reports found.</span></center>
                    </EmptyDataTemplate>
                </asp:GridView>

now when i click on second column (RefNo) some value (123) i want the header text RefNo in code behind.. thank you...

I may have missed the point of why you're trying to get the Header Column Name but it appears your just trying to get the command method to action differently depending on the button clicked.

The simplest way to do this is to use the CommandName property to define the action required, you already know what the buttons are meant to do so there's no need to complicate matters any further.

So :

<asp:LinkButton ID="lnkRefNo" runat="server" CommandName="Ref" CommandArgument='<%# Bind("LetterID") %>' Text='<%# Eval("RefNo") %>'  />

and

<asp:LinkButton ID="lnkSubject" CommandName="Subject" CommandArgument='<%# Bind("LetterID") %>' Text='<%# Bind("Subject") %>' runat="server" />

In the codebehind:

private void grdReport_RowCommand(object sender, CommandEventArgs e)
{
    string commandName = e.CommandName;
    string commandArg = e.CommandArgument.ToString();
    switch (commandName)
    {
        case ("Ref"):
            //do whatever for Ref
            break;
        case ("Subject"):
            //whatever for Subject
            break;
        default:
            throw new NotImplementedException();
            break;
    }
}

Please find the code ,hope it can help you.

Index.aspx

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="OnSelectingGetHeaderName.Index" %>

         <!DOCTYPE html>

          <html xmlns="http://www.w3.org/1999/xhtml">
          <head runat="server">
          <title></title>
          </head>
          <body>
<form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
        AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
            <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
        </Columns>
    </asp:GridView>
    <br />
    <u>Selected Row Values: </u>
    <br />
    <br />
    <asp:Label ID="lblValues" runat="server" Text=""></asp:Label>
</form>

Index.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string name = GridView1.HeaderRow.Cells[0].Text +":"+GridView1.SelectedRow.Cells[0].Text;
        string country = GridView1.HeaderRow.Cells[1].Text + ":"+(GridView1.SelectedRow.FindControl("lblCountry") as Label).Text;
        lblValues.Text =  name  +' '+country;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM