简体   繁体   English

在GridView C#中获取选定的列标题文本

[英]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. 在我的asp.net应用程序中,我有一个带有两个链接按钮作为两列的gridview。 and in gridview had some rows , now my question is how to get the column header text when a column value is selected. 并且在gridview中有一些行,现在我的问题是当选择列值时如何获取列标题文本。

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. 就像我在gridview中有五个列(A,B,C,D和E),其中(B和E)是链接按钮,并且gridview绑定了一些行。 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 现在,如果我在列“ B”中选择一个值(X),我想在代码后方(类似于E列)中获得列标题文本“ B”。必须区分所选的列标题文本。 ..这是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... 现在,当我单击第二列(RefNo)某个值(123)时,我希望标题文本RefNo在后面的代码中..谢谢...

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. 最简单的方法是使用CommandName属性定义所需的操作,您已经知道按钮的用途,因此无需进一步复杂化。

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 的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 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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