如何通过 DataField 在 GridView 中找到 BoundField?

How can I find a BoundField inside a GridView by DataField?

我想更改我的 GridView 中第十个 BoundField 后面的 CSS class,但我想通过 DataField 找到它(即使用字符串作为索引)。

protected void gdDeliveryDates_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string DeliveryDue = DataBinder.Eval(e.Row.DataItem, "DeliveryDue").ToString();
        ((LinkButton) e.Row.FindControl("PostDelivery")).Enabled = !String.IsNullOrEmpty(DeliveryDue);

        //e.Row.Cells[9].CssClass= "badge";
    }
}

在上面的代码中,我注释掉了迄今为止我找到的唯一解决方案,这对我来说是不可接受的,因为列号(即上面指定的第 9 列)非常不稳定。我更愿意通过 DataField(字符串,在本例中为 "MailCount",正如您将在后面的网格声明中看到的那样)查找列。下面是我的网格的样子:

<asp:GridView ID="gdDeliveryDates" runat="server" AllowPaging="False" AllowSorting="True" DataSourceID="odsDeliveryDates" AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-hover" OnRowCommand="gdDeliveryDates_RowCommand" OnSelectedIndexChanged="gdDeliveryDates_SelectedIndexChanged" DataKeyNames="PackageOfferedID, PackageID, PostageID, PackageNumber, PackageTitle, PostageName, Section, PostageStart, PostageEnd, DeliveryDue, LName, MailCount, Location" OnRowDataBound="gdDeliveryDates_RowDataBound" >
    <Columns>

        <asp:BoundField DataField="PackageID" HeaderText="PackageID" Visible="False" ReadOnly="True" SortExpression="PackageID" />
        <asp:BoundField DataField="PackageOfferedID" HeaderText="PackageOfferedID" Visible="False" ReadOnly="True" SortExpression="PackageOfferedID" />
        <asp:BoundField DataField="PostageID" HeaderText="PostageID" Visible="False" ReadOnly="True" SortExpression="PostageID" />

        <asp:BoundField DataField="PackageNumber" HeaderText="Package" Visible="True" ReadOnly="True" SortExpression="PackageNumber" HeaderStyle-CssClass="visible-xs visible-sm visible-md visible-lg" ItemStyle-CssClass="visible-xs visible-sm visible-md visible-lg" />

        <asp:BoundField DataField="PostageName" HeaderText="Postage" ReadOnly="True" SortExpression="PostageName" HeaderStyle-CssClass="visible-sm visible-md visible-lg" ItemStyle-CssClass="visible-sm visible-md visible-lg"/>
        <asp:BoundField DataField="Section" HeaderText="Section" ReadOnly="True" SortExpression="Section" HeaderStyle-CssClass="visible-xs visible-sm visible-md visible-lg" ItemStyle-CssClass="visible-xs visible-sm visible-md visible-lg" />

        <asp:BoundField DataField="PostageStartDate" HeaderText="Start Date" ReadOnly="True" SortExpression="PostageStartDate" DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-CssClass="visible-md visible-lg" ItemStyle-CssClass="visible-md visible-lg" />
        <asp:BoundField DataField="PostageEndDate" HeaderText="End Date" ReadOnly="True" SortExpression="PostageEndDate" DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-CssClass="visible-md visible-lg" ItemStyle-CssClass="visible-md visible-lg" />
        <asp:BoundField DataField="DeliveryDueDate" HeaderText="Delivery Due" ReadOnly="True" SortExpression="DeliveryDueDate" DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-CssClass="visible-xs visible-sm visible-md visible-lg" ItemStyle-CssClass="visible-xs visible-sm visible-md visible-lg" />

        <asp:BoundField DataField="MailCount" HeaderText="#" Visible="True" ReadOnly="True" SortExpression="MailCount" HeaderStyle-CssClass="visible-lg" ItemStyle-CssClass="visible-lg" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="PostDelivery" runat="server" CausesValidation="false" CommandName="Add"
                            Text="Post Delivery" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
                            CssClass="buttonLayout" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="LName" HeaderText="LName" Visible="False" ReadOnly="True" SortExpression="LName" />

        <asp:BoundField DataField="Location" HeaderText="Location" Visible="False" ReadOnly="True" SortExpression="Location" />
    </Columns>
</asp:GridView>

这是不可能的,但你可以试试这个方法:

public int FindIndexByDataField(this GridView gv, string datafieldname)
{
    int index = -1, cnum = 0;
    foreach (DataControlField col in gv.Columns)
    {
        if (col is BoundField)
        {
            BoundField coll = (BoundField)gv.Columns[cnum];
            if (coll.DataField == datafieldname)
            {
                index = cnum;
                break;
            }
        }
        cnum++;
    }
    return index;
}

然后像这样调用上面的方法:

e.Row.Cells[FindIndexByDataField("MailCount")].CssClass= "badge";