C# 根据数据更改gridview中行的颜色

C# 根据数据更改gridview中行的颜色,c#,asp.net,gridview,C#,Asp.net,Gridview,我在ASP.Net-C中有一个Gridview。我有一列名为作业或考试。在该列中,我有作业或考试的名称,例如:“考试1”,“作业5”我希望作业的每一行都是红色,考试是蓝色 在SQL Server或我的代码中,最好的方法是什么?如果是,正确的代码是什么?首先在变量中查找行索引。在下面的代码中,我使用index作为变量 grdWithLocation.Rows[index].BackColor = Color.FromArgb(255, 238, 238, 238); 通过分别为每一行设置Back

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


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

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

grdWithLocation.Rows[index].BackColor = Color.FromArgb(255, 238, 238, 238);

通过分别为每一行设置
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();
}

受保护的无效页面加载(对象发送方、事件参数e)
{
DataTable测试=新DataTable();
tests.Columns.Add(新数据列(“类型”));
tests.Columns.Add(新数据列(“名称”));
tests.AcceptChanges();
tests.Rows.Add(新[]{“赋值”,“堆栈溢出基础”});
tests.Rows.Add(新[]{“考试”,“专家评分”});
tests.Rows.Add(new[]{“Exam”,“Upvoting”});
tests.Rows.Add(new[]{“Assignment”,“Rep Changes”});
TestGridView.DataSource=测试;
TestGridView.DataBind();
}
在事件的代码中,我们可以获得绑定到的单个数据行并检查值,因此可以相应地设置背景色:

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;
        }
    }
}
protectedvoid TestGridView\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
//忽略作为标题的第一行
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//获取该行,然后获取它绑定到的数据行
GridViewRow行=e.行;
DataRow数据=((DataRowView)row.DataItem).row;
//查看值并相应地设置颜色
开关(数据字段(“类型”))
{
案例“转让”:
row.BackColor=System.Drawing.Color.FromName(“蓝色”);
打破
案例“考试”:
row.BackColor=System.Drawing.Color.FromName(“红色”);
打破
}
}
}

这很好,虽然你可能也想考虑把文本颜色设置为白色,这样读起来稍微容易一些。 但是,将来您可能需要更大的灵活性,例如,如果您添加了第三种评估类型,即绿色的“实验室”,则需要更改/重新编译/重新测试/重新部署代码。相反,如果您从数据库向上传递命名颜色,然后在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");
    }
}
protectedvoid TestGridView\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
//忽略作为标题的第一行
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//获取该行,然后获取它绑定到的数据行
GridViewRow行=e.行;
DataRow数据=((DataRowView)row.DataItem).row;
row.BackColor=System.Drawing.Color.FromName(data.Field(“BackColor”);
row.ForeColor=System.Drawing.Color.FromName(data.Field(“TextColour”);
}
}

我在我的库存数据库中使用它。它检查它是什么日期,并将它与数据库中每个订单的日期进行比较。然后,它根据每行的存放时间为每行添加不同的颜色。它位于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;
        }
    }
}
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);
字符串strMediaX=e.Row.Cells[2]。文本;
if(信息IsDate(strMediaX))
{
DateTime MediaX=e.Row.Cells[2]。文本;
中频(中位数
问题是我不知道行名,在“作业或考试”列中,每当它是“作业”时,行都是红色的…如果列下的单元格是作业,我怎么说…@Pedramrafi您的源数据中有什么字段?它只是一个名称吗,例如作业1、考试3?