【问题标题】:How can a color-coded interpretation be provided based on the results?如何根据结果提供颜色编码的解释?
【发布时间】:2021-08-16 09:03:29
【问题描述】:

我有一个包含特定信息的数据库 我想用适当的文本值显示许多值中的一些值并具有特殊的颜色 这就是我所拥有的以及我希望它出现在浏览器中的内容 . 带着爱,谢谢你的帮助

   public partial class CS : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!this.IsPostBack)
       {
           this.BindGrid();
       }
   }
   private void BindGrid()
   {
       {
           using (SqlDataAdapter sda = new SqlDataAdapter("SELECT TOP 10 id, name, cood FROM test20 WHERE name NOT LIKE '%@%' ORDER BY id DESC", con))
           {       
               using (DataTable dt = new DataTable())
               {
                   sda.Fill(dt);
                   GridView1.DataSource = dt;
                   GridView1.DataBind();                                                           
               }
           }
       }
       GridView1.UseAccessibleHeader = true;
       GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
   }
}

The current output is similar to this

The desired output is similar to this

【问题讨论】:

标签: c# asp.net


【解决方案1】:

嗯,你没有说你打算从哪里得到这些颜色?

我的意思是,你的颜色需要有一些逻辑。但是,一旦你有了一些设置颜色的方法,那么解决方案就相当简单了。

比如说,我有一张酒店表格。

对于 City = Jasper,我想要绿色,对于 Banff,我想要蓝色。

所以,说这个网格视图:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table table-hover">
            <Columns>
                <asp:BoundField DataField="FirstName"   HeaderText="FirstName" />
                <asp:BoundField DataField="LastName"    HeaderText="LastName"  />
                <asp:BoundField DataField="HotelName"   HeaderText="HotelName"  />
                <asp:BoundField DataField="City"        HeaderText="City"  />
                <asp:BoundField DataField="Description" HeaderText="Description" />
            </Columns>
        </asp:GridView>

以及加载上述网格的代码?

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGrid();
        }
    }

    void LoadGrid(string strCity = "")
    {
        using (SqlCommand cmdSQL = new SqlCommand("SELECT * FROM tblHotels",
                         new SqlConnection(Properties.Settings.Default.TEST3)))
        {
            cmdSQL.Connection.Open();
            GridView1.DataSource = cmdSQL.ExecuteReader();
            GridView1.DataBind();
        }
    }

因此输出是这样的:

现在,要上色吗?那么我们可以使用 RowDataBound 事件,像这样:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            switch (e.Row.Cells[3].Text)
            {
                case "Jasper":
                    {
                        e.Row.Cells[3].ForeColor = System.Drawing.Color.Blue;
                        break;
                    }

                case "Banff":
                    {
                        e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
                        break;
                    }
            }
        }
    }

现在我们的输出如下所示:

但是,如果您的 gv 使用默认列或绑定字段列,您是否遗漏了 HUGE WHOPPER 详细信息?如果是,那么上面会正常工作。

如果您使用模板列,则需要使用不同的方法。

因此,假设 City 是一个标准的 say 标签,如下所示:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table table-hover" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="FirstName"   HeaderText="FirstName"   />
                <asp:BoundField DataField="LastName"    HeaderText="LastName"    />
                <asp:BoundField DataField="HotelName"   HeaderText="HotelName"   />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="txtCity" runat="server" Text= '<%# Eval("City") %>' > </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Description" HeaderText="Description" />
            </Columns>
        </asp:GridView>

那么我们的 RowDatabound 代码如下所示:

       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label txtCity = (Label)e.Row.FindControl("txtCity");

            switch (txtCity.Text)
            {
                case "Jasper":
                    {
                        txtCity.ForeColor = System.Drawing.Color.Blue;
                        break;
                    }

                case "Banff":
                    {
                        txtCity.ForeColor = System.Drawing.Color.Green;
                        break;
                    }
            }
        }

最后但并非最不重要?如果我有 20 个不同的城市和 20 种不同的颜色,当然不会有人对颜色进行硬编码。

所以,我会说制作/拥有/创建/使用城市表,并说出我想要的颜色,然后将其加入查询中。在这种情况下,我会像您一样使用表绑定网格,因为绑定期间的数据源甚至可以使用数据源中的任何列。如果您像我一样使用 ExecuteReader,那么您仅限于网格中的列,而不是 gv 数据源中的其他列。 (例如在这个想法颜色中)。这意味着如果您要使用未出现在网格中的列(例如在这个想法中 - 颜色不会是 gv 显示中的列,但是它将在表数据源中,因此可以在绑定事件期间使用)。

编辑 ---------------------------------- em>

所以,代替在代码中硬编码颜色,我们可以这样做:

当然,我们使用了这里的颜色:

https://en.wikipedia.org/wiki/Web_colors

好的,现在我们将上述颜色表中的简单 LEFT 连接到我们的查询中。

请注意,我们必须使用表,因为在数据绑定期间,我需要/想要充分使用数据行 - 即使是我不想显示的列。所以在下面的标记中,我现在可以删除城市列,但仍然在数据绑定中使用城市。 (但是,嘿,让我们把它留给这篇文章吧。

所以,我的标记列现在是这样的:

                <asp:TemplateField HeaderText="Color">
                    <ItemTemplate>
                        <asp:Label ID="txtColor" runat="server" Text= '<%# Eval("Color") %>' Font-Bold="true"> </asp:Label>
                    </ItemTemplate>

(其余同前)。

现在我的查询+数据绑定代码是这样的:

        using (SqlCommand cmdSQL = new SqlCommand("SELECT *,Color FROM tblHotels "
            + "LEFT JOIN tblCityColors on tblHotels.City = tblCityColors.City " 
            + "ORDER BY HotelName",
                         new SqlConnection(Properties.Settings.Default.TEST3)))
        {
            cmdSQL.Connection.Open();
            DataTable rst = new DataTable();
            rst.Load(cmdSQL.ExecuteReader());
            GridView1.DataSource = rst;
            GridView1.DataBind();
        }

现在我们的 Row 数据绑定事件是这样的:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label txtCity = (Label)e.Row.FindControl("txtCity");
            Label txtColor = (Label)e.Row.FindControl("txtColor");
            DataRowView dr = (DataRowView)e.Row.DataItem;

            txtColor.ForeColor = System.Drawing.Color.FromName(dr["Color"].ToString());
            txtCity.ForeColor = System.Drawing.Color.FromName(dr["Color"].ToString());
        }
    }

如前所述,我们可以从上面删除城市列 - 但对于这个演示,没关系。

现在我们的输出是这样的:

请注意我们在绑定事件期间如何获取 FULL 数据行:

  DataRowView dr = (DataRowView)e.Row.DataItem;

因此,GridView 数据源仅在数据绑定事件期间有效,并且仅在活动期间有效。渲染网格后,DataItem(用于行)或 GridView.DataSource 为 null 且无效 - 它仅在绑定事件期间存在,但这就是我们需要它的全部。

因此,我们可以使用 SQL 中的任何和所有列——甚至是那些未显示在网格中的列。

【讨论】:

  • 感谢您的回复 是的,我受益于您的指示并且能够更改颜色 我也想更改颜色名称中的 many 值,我不希望出现数字如示例中的数字310变成单词(红色)并且也是红色的..等等
  • 好吧,那么您需要某种翻译表 - 请参阅我上面的编辑 - 我展示了我们如何拥有多种颜色 - 不是在代码中硬编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多