如果gridview中的值为空,我有更改gridview颜色的问题。我使用templatefield生成gridview。我使用了这段代码,除了标题之外,它在gridview中全部更改:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//change it according your cell number or find element
if (e.Row.Cells[0].Text != "")
e.Row.BackColor = System.Drawing.Color.White;
else
e.Row.BackColor = System.Drawing.Color.Red;
}
}
这是我的后台代码,我指定gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Request Date">
<ItemTemplate><%#Eval("request_date")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A01">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A01" ? "Job Order:"
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\n\nNeed by Date:"
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A02">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A02" ?"Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A03">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A03" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A04">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A04" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A05">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A05" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A06">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A06" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A07">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A07" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A08">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A08" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A09">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A09" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A10">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A10" ?"Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A11">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A11" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A12">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A12" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A13">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A13" ?"Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A14">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A14" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A15">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A15" ?"Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A16">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A16" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A17">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A17" ?"Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "
+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="A18">
<ItemTemplate>
<%#Eval("prod_line").ToString() == "A18" ? "Job Order: "
+Eval("jo_no") +"\n\n\n\n\nQuantity: "
+Eval("qty")+"\n\n\n\nNeed by Date: "
+Eval("need_by_date")+"\n\n\n\n\nStatus: "+Eval("process_id") : "" %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我得到了这样的输出:
但我希望输出如下:
答案 0 :(得分:0)
查看代码的这一部分,您将找到问题
if (e.Row.Cells[0].Text != "")
e.Row.BackColor = System.Drawing.Color.White;
else
e.Row.BackColor = System.Drawing.Color.Red;
如果单元格为空,则设置整行 [e.Row.BackColor] 的背景颜色
在行中设置单元格的颜色
像这样
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
你可以用它来应用你的逻辑(无论你想要哪个颜色)
答案 1 :(得分:0)
据我了解这个问题,您的单元格没有任何价值,但如果您通过放置断点在RowDataBound
事件处理程序中看到它,您可能会看到像
这样的值而不是空字符串。
您只需要在RowDataBound
内稍微修改您的条件,如下所示:
if (string.IsNullOrEmpty(Server.HtmlDecode(e.Row.Cells[1].Text).Trim()))
e.Row.BackColor = System.Drawing.Color.Red;
单元格中的值为htmlEncoded
。您需要使用Server.HtmlDecode
对其进行解码,之后您的值为" "
字符串,其中包含一个空格,这就是我使用Trim()
的原因,最后检查该值是否为null or empty
这是经过测试的解决方案,它可以正常工作。