如果未知的列数量,如何在GridView中动态彩色单元格



i具有一个动态生成的GridView,其中列数量未知。单元格中的数据为1和0(矩阵类型值)。有没有一种方法可以使绿色1s和0s为白色的细胞上色?

asp.net中的代码:

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<!DOCTYPE html>    
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>    
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" /> 
    <style type="text/css">
        .auto-style1 {
            height: 223px;
            text-align: center;
            margin-top: 0px;
        }
        .auto-style2 {
            text-align: left;
        }
        .auto-style3 {
            float: left;
        }
        .auto-style4 {
            margin-top: 0px;
        }
        .auto-style5 {
            height: 8px;
            margin-top: 0px;
        }
        .auto-style6 {
            height: 8px;
        }
        .auto-style7 {
            font-size: medium;
        }
        .auto-style8 {
            float: left;
            margin-left: 9px;
        }
        .auto-style10 {
            float: left;
            width: 768px;
            height: 61px;
            margin-left: 9px;
            margin-top: 9px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="auto-style1">
    <div class="auto-style28">    
        <img class="auto-style10" src="Images/Channel%20Map.png" /><br />
        <br />
        <br />
        <br />
    <hr width="Auto" class="auto-style5" />
        <table class="auto-style4">
            <tr>
                <td class="auto-style2"><span style="font-family: Arial">
                    &nbsp;<span class="auto-style7">&nbsp; FILTER</span>:<br />
                    <br />
                    <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" CssClass="auto-style8" Height="25px"  Width="240px" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" style="font-size: medium">
                        <asp:ListItem Text="Select Item" Value="-1"></asp:ListItem>
                        <asp:ListItem Text="Region     " Value="0"></asp:ListItem>
                        <asp:ListItem Text="HQ         " Value="1"></asp:ListItem>
                        <asp:ListItem Text="DMA        " Value="2"></asp:ListItem>
                        <asp:ListItem Text="Company    " Value="3"></asp:ListItem>
                        <asp:ListItem Text="Machine    " Value="4"></asp:ListItem>
                        <asp:ListItem Text="Zone       " Value="5"></asp:ListItem>
                    </asp:DropDownList>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:DropDownList ID="ddl2" runat="server" CssClass="auto-style15" Height="25px" Margin-Left="-12px" Width="250px" AutoPostBack="False" style="font-size: medium" >
                    </asp:DropDownList>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </span> 
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Run Report" Width="104px" CssClass="auto-style25" />
                    &nbsp;&nbsp;&nbsp;
                    <br />
                    <br />
                    <span class="auto-style3"><span class="auto-style7" style="color: rgb(0, 0, 0); font-size: medium; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span><span class="auto-style23">&nbsp;&nbsp; <asp:Label ID="Label2" runat="server" Text="CHANNELS FOUND: "></asp:Label>
                    &nbsp;<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                    </span></span></span></span>&nbsp; </td>
            </tr>
        </table>
    </div>
    <hr width="Auto" class="auto-style6" />
        <asp:GridView id="GridView1" runat="server"  DataSourceID="SqlDataSource1"  OnRowDataBound="GridView1_RowDataBound" OnDataBound="gv_DataBound"   Width="274px" EnableModelValidation="True" HorizontalAlign="Left" CssClass="rwd-table">
            <AlternatingRowStyle Wrap="False" />
            <EditRowStyle Wrap="False" /> 
            <HeaderStyle Width="75px" BorderStyle="Inset" BorderWidth="3px" Wrap="False" />
            <RowStyle Wrap="False" />
            <SelectedRowStyle Wrap="False" />
        </asp:GridView>
    <asp:SqlDataSource id="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConString %>" 
     SelectCommand="Usp_GetWHEREfilter" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:FormParameter DefaultValue="" FormField="ddl2" Name="Filter" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
        </div>
    </form>
</body>
</html>

这是我表的示例:

具有1NS的细胞 - 应为绿色具有0s的单元格 - 应为白色

______________________________________
COLUMNS  n1 n2 n3 n4 n5 n6 ... n
VALUE1   1  0  1  0  0  1  ... 0 
VALUE2   0  0  1  0  1  1  ... 1
VALUE3   0  1  1  0  0  1  ... 1
VALUE4   1  1  1  0  0  1  ... 0
.....    .  .  .  .  .  .  ... .
VALUEn   1  0  1  1  0  1  ... 0
______________________________________

感谢您的时间和帮助!!!

您可以为此使用OnRowdDataBound事件。如果您不知道数据集中的列数量,则必须循环每个单元格并检查其值。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the rowtype is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string lastLetter = string.Empty;
        //check if cell 1 actually contains data, otherwise substring will fail
        if (!string.IsNullOrEmpty(e.Row.Cells[0].Text))
        {
            //get the last letter of the string in cell 1
            lastLetter = e.Row.Cells[0].Text.Substring(e.Row.Cells[0].Text.Length - 1, 1).ToUpper();
        }
        //loop all the cells in the row
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            int value = 0;
            //try converting the cell value to an int
            try
            {
                value = Convert.ToInt32(e.Row.Cells[i].Text);
            }
            catch
            {
            }
            //check the value and set the background color
            if (value == 1 && lastLetter == "H")
            {
                e.Row.Cells[i].BackColor = Color.Green;
            }
            else if (value == 1 && lastLetter == "N")
            {
                e.Row.Cells[i].BackColor = Color.Blue;
            }
            else
            {
                e.Row.Cells[i].BackColor = Color.White;
            }
        }
    }
}

在rowdatabound事件上,尝试。

{
           if (DataBinder.Eval(e.Row.DataItem, "n1") == 0)
            {
                e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
            }
            else
            {
                e.Row.Cells[1].BackColor = System.Drawing.Color.White;
            }
}

最新更新