1

我想更改我的 GridView 中第十个 BoundField 后面的 CSS 类,但我想通过 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>
4

1 回答 1

2

这是不可能的,但您可以尝试以下方法:

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";
于 2017-09-29T15:34:49.273 回答