2

我在 ASP.Net - C# 中有一个 Gridview。我有一列名为Assignmentor Exam。在该列中,我有作业或考试的名称,例如:"Exam 1""Assignment 5" 我希望作为作业的每一行都应该是红色的,而考试是蓝色的。

在 SQL Server 或我的代码中,最好的方法是什么?如果是这样,正确的代码是什么?

4

3 回答 3

2

BackColor您可以通过单独设置每一行的属性来设置 Gridview 中一行的背景颜色。要根据行中的数据执行此操作,您需要在绑定时检查行,您可以在RowDataBound事件中执行此操作。这是一个基本 Gridview 的快速标记,我们在其中连接到服务器端事件:

<asp:GridView runat="server" AutoGenerateColumns="False" OnRowDataBound="TestGridView_RowDataBound" ID="TestGridView">
    <Columns>
        <asp:BoundField DataField="Type" HeaderText="Assignment/Exam" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    DataTable tests = new DataTable();
    tests.Columns.Add(new DataColumn("Type"));
    tests.Columns.Add(new DataColumn("Name"));
    tests.AcceptChanges();

    tests.Rows.Add(new []{"Assignment","StackOverflow Basics"});
    tests.Rows.Add(new[]{"Exam","Expert Markdown"});
    tests.Rows.Add(new[]{"Exam","Upvoting"});
    tests.Rows.Add(new[]{"Assignment","Rep Changes"});

    TestGridView.DataSource = tests;
    TestGridView.DataBind();
}

在事件的代码中,我们可以获取我们绑定到的单个数据行并检查值,因此我们可以相应地设置 BackColor:

protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Ignore the first row which is the header
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get hold of the row and then the DataRow that it's being bound to
        GridViewRow row = e.Row;
        DataRow data = ((DataRowView)row.DataItem).Row;

        // Look at the value and set the colour accordingly
        switch (data.Field<string>("Type"))
        {
            case "Assignment":
                row.BackColor = System.Drawing.Color.FromName("Blue");
                break;
            case "Exam":
                row.BackColor = System.Drawing.Color.FromName("Red");
                break;
        }
    }
}

效果很好,尽管您可能还想考虑将文本颜色设置为白色,以便阅读。

但是,您将来可能需要更多的灵活性,例如,如果您添加第三种评估类型,称为“实验室”,颜色为绿色,您需要更改/重新编译/重新测试/重新部署代码。相反,如果您从数据库向上传递一个命名颜色,然后在 RowDataBound 事件中使用它,则可以避免其中的一些工作,例如:

protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Ignore the first row which is the header
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get hold of the row and then the DataRow that it's being bound to
        GridViewRow row = e.Row;
        DataRow data = ((DataRowView)row.DataItem).Row;

        row.BackColor = System.Drawing.Color.FromName(data.Field<string>("BackColour");
        row.ForeColor = System.Drawing.Color.FromName(data.Field<string>("TextColour");
    }
}
于 2012-11-26T12:06:32.960 回答
0

首先在变量中找到行索引。在下面的代码中,我使用 index 作为变量。

grdWithLocation.Rows[index].BackColor = Color.FromArgb(255, 238, 238, 238);
于 2012-11-26T11:54:14.437 回答
0

我在我的库存数据库中使用它。它会检查日期,并将其与数据库中每个订单的日期进行比较。然后根据他们坐了多长时间,它用不同的颜色为每一行着色。它在 GridView 的 RowDataBound 上

您应该能够修改代码以将日期更改为考试/作业

if (e.Row.RowType == DataControlRowType.DataRow)
{
    DateTime datToday = DateTime.Today();
    DateTime O1 = datToday.AddDays(0);
    DateTime O2 = datToday.AddDays(-1);
    DateTime O3 = datToday.AddDays(-4);

    string strMediaX = e.Row.Cells[2].Text;

    if (Information.IsDate(strMediaX))
    {
        DateTime MediaX = e.Row.Cells[2].Text;
        if (MediaX < O3)
        {
            e.Row.Cells[2].BackColor = Drawing.Color.OrangeRed;
            e.Row.Cells[2].ForeColor = Drawing.Color.White;
        }
        else if (MediaX < O2)
        {
            e.Row.Cells[2].BackColor = Drawing.Color.Orange;
            e.Row.Cells[2].ForeColor = Drawing.Color.White;
        }
        else if (MediaX < O1)
            e.Row.Cells[2].BackColor = Drawing.Color.Gold;
    }
    // checks the current stock of the item and if it is below 10 then it changes the colour to highlight that it needs to be ordered.
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((Label)e.Row.Cells[9].FindControl("lblCurrentStock").Text < 11)
        {
            e.Row.Cells[9].BackColor = System.Drawing.Color.OrangeRed;
            e.Row.Cells[9].ForeColor = Drawing.Color.White;
            e.Row.Cells[9].Font.Bold = true;
        }
    }
}
于 2019-02-27T16:06:39.710 回答