我有一个下拉式cboVendor,其中供应商名称正在其中
  我希望背景颜色为红色,其fullyAgg(dt contains 11 columns in which fullagg is the 11th column)即将为零。当前我正在执行以下代码所示的操作,但它将所有这些都设为零(这不应该发生)


.aspx

<asp:DropDownList ID="cboVendor" runat="server" AppendDataBoundItems="True"
AutoPostBack="true"> <asp:ListItem Value="0">- Select Vendor -</asp:ListItem>
</asp:DropDownList>


C#代码

DataTable dt = default(DataTable);
cboVendor.DataSource = dt;
cboVendor.DataTextField = "SupplierName";
cboVendor.DataValueField = "SupplierID";
cboVendor.DataBind();
cboVendor.SelectedIndex = 0;
foreach (ListItem item in cboVendor.Items) {
    if (dt.Rows(10)("fullyAgg") == 0) {
        item.Attributes.Add("style", "background-color:red;");
    }
}

最佳答案

找到了解决方案

DataView dv = dt.DefaultView;
dv.RowFilter = "fullyAgg=0";
foreach (DataRowView dr in dv) {
    foreach (ListItem item in cboVendor.Items) {
        if (dr("SupplierID").ToString() == item.Value.ToString()) {
            item.Attributes.Add("style", "background-color:red;");
        }
    }
}

07-24 16:49