【问题标题】:Visual Studios 2015 Gridview Conditional FormattingVisual Studios 2015 Gridview 条件格式
【发布时间】:2016-01-27 00:07:14
【问题描述】:

我对 C# 还很陌生,所以我不太擅长编码。目前,我正在使用 Visual Studios Community 2015 并且主要在设计视图中工作。

我创建了一个绑定到允许用户更新记录的 SQL 数据源的 gridview。

现在我想做的是:

我有 2 列。服务器和状态。我想根据给定的文本突出显示状态列。

例如:Status=done,应该以绿色突出显示。

实现这一目标的最佳方法是什么?有人可以指出我正确的方向,无论是链接指南还是帮助背后的代码。

谢谢,

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="SID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" ReadOnly="True" SortExpression="SID" />
                <asp:BoundField DataField="Servers" HeaderText="Servers" SortExpression="Servers" />
                <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:it_ConnectionString2 %>" SelectCommand="SELECT * FROM [Main_Status_test]" UpdateCommand="Update [Main_Status_test] Set [Status]=@Status Where [SID]=@SID"></asp:SqlDataSource>
    </form>
</body>
</html>

后面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    {
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.Cells[3].Text == "done")
        {
            row.BackColor = System.Drawing.Color.Green;
        }
        else if (row.Cells[3].Text == "not done")
        {
            row.BackColor = System.Drawing.Color.Red;
        }
    }
}

【问题讨论】:

  • 能贴出gridview的标记代码吗?
  • 是的,这是个好主意。我会尽快更新。
  • 听起来您只是想检查状态并相应地更改颜色?
  • 是的,差不多就是这样。如果状态为“完成”,则应将记录突出显示为绿色,如果状态为“未完成”,则应将记录突出显示为红色。

标签: c# visual-studio gridview


【解决方案1】:

只需将以下代码添加到 GridView1 DataBound 事件。

foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.Cells[2].Text == "done")
            {
                row.BackColor = System.Drawing.Color.Green;
            }
            else if (row.Cells[2].Text == "WHATEVER YOU WANT")
            {
             row.BackColor = System.Drawing.Color.'Whatevercoloryouwant'
            }
            //etc...
        }

【讨论】:

  • 太棒了,这行得通。您提供的代码中有一些小错误。您将如何仅对状态列执行此操作?我现在正在研究这个。非常感谢!
  • 很高兴它成功了。 row.Cells[2] 指定该行中的第三列(使用索引 [2])。如果您想选择不同的列,只需将“2”更改为您要检查的任何列索引。不要忘记标记为答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
相关资源
最近更新 更多