我在ASP.Net中有一个Gridview - C#。我有一个名为Assignment
或Exam
的专栏。在那一栏中,我有作业或考试的名称,例如:"Exam 1"
,"Assignment 5"
我希望作为作业的每一行都应该是红色并且考试是蓝色的。
在SQL Server或我的代码中,最好的方法是什么?如果是这样,什么是正确的代码?
您可以通过分别为每一行设置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;
}
}
}
虽然您可能还想考虑将文本颜色设置为白色,因此它更容易阅读。
但是,您可能希望将来有更多的灵活性,例如如果您添加第三个名为“Lab”的评估类型为绿色,则需要更改/重新编译/重新测试/重新部署代码。相反,如果你从数据库中传递一个命名颜色然后在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");
}
}
首先在变量中找到行索引。在下面的代码中,我使用了index作为变量。
grdWithLocation.Rows[index].BackColor = Color.FromArgb(255, 238, 238, 238);
我在库存数据库中使用它。它检查它的日期,并将其与每个订单的数据库中的日期进行比较。然后根据它们坐了多长时间,用不同的颜色为每一行着色。它位于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;
}
}
}