在gridview c#

时间:2016-02-19 12:16:31

标签: c# asp.net gridview

在我的asp.net应用程序中,我有一个gridview,其中两个链接按钮为两列。 并且在gridview中有一些行,现在我的问题是如何在选择列值时获取列标题文本。

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

现在当我点击第二列(RefNo)某个值(123)时,我希望代码后面的标题文本RefNo .. 谢谢......

2 个答案:

答案 0 :(得分:1)

我可能已经错过了你为什么要尝试获取标题列名称这一点,但看起来你只是试图让命令方法根据点击的按钮采取不同的行动。

最简单的方法是使用<asp:LinkButton ID="lnkRefNo" runat="server" CommandName="Ref" CommandArgument='<%# Bind("LetterID") %>' Text='<%# Eval("RefNo") %>' /> 属性来定义所需的操作,您已经知道了按钮的用途,因此无需进一步复杂化。< / p>

所以:

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

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

在代码隐藏中:

<mvc:annotation-driven />

<!-- Swagger config bean -->
<bean class="<---package--->.SwaggerConfig"/>

答案 1 :(得分:0)

请找到代码,希望它可以帮到你。

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