我在ASP.Net中有一个Gridview - C#。我有一个名为Assignment
或Exam
的列。在该列中,我有作业或考试的名称,例如:"Exam 1"
,"Assignment 5"
我希望作为作业的每一行都应为红色且考试为蓝色。
在SQL Server或我的代码中执行此操作的最佳方法是什么?如果是这样,那么正确的代码是什么?
答案 0 :(得分: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;
}
}
}
虽然您可能还想考虑将文字颜色设置为白色,但这样会更容易阅读。
但是,您可能希望将来有更多的灵活性,例如如果您添加第三个名为“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");
}
}
答案 1 :(得分:0)
首先在变量中找到行索引。在下面的代码中,我使用了index作为变量。
grdWithLocation.Rows[index].BackColor = Color.FromArgb(255, 238, 238, 238);
答案 2 :(得分:0)
我在库存数据库中使用它。它会检查日期,并将其与数据库中每个订单的日期进行比较。然后根据每行坐了多长时间,为每行上色。它在GridView的RowDataBound上
您应该能够修改代码以更改考试/作业的日期
energy = read.table(header = T, text = "country year sector UN ETS
BG 2000 Energy 24076856.07 NA
BG 2001 Energy 27943916.88 NA
BG 2002 Energy 25263464.92 NA
BG 2003 Energy 27154117.22 NA
BG 2004 Energy 26936616.77 NA
BG 2005 Energy 27148080.12 NA
BG 2006 Energy 27444820.45 NA
BG 2007 Energy 30789683.97 31120644
BG 2008 Energy 32319694.49 30453798
BG 2009 Energy 29694118.01 27669012
BG 2010 Energy 31638282.52 29543392
BG 2011 Energy 36421966.96 34669936
BG 2012 Energy 31628708.27 30777290
BG 2013 Energy 27332059.98 27070570
BG 2014 Energy 29036437.07 28583008
BG 2015 Energy 30316871.19 29935784
BG 2016 Energy 27127914.93 26531704
BG 2017 Energy NA 27966156
CH 2000 Energy 3171899.5 NA
CH 2001 Energy 3313509.6 NA
CH 2002 Energy 3390115.69 NA
CH 2003 Energy 3387122.65 NA
CH 2004 Energy 3682404.04 NA
CH 2005 Energy 3815915.41 NA
CH 2006 Energy 4031766.36 NA
CH 2007 Energy 3718892.16 NA
CH 2008 Energy 3837098.91 NA
CH 2009 Energy 3673731.74 NA
CH 2010 Energy 3846523.62 NA
CH 2011 Energy 3598219.48 NA
CH 2012 Energy 3640743.25 NA
CH 2013 Energy 3735935.29 NA
CH 2014 Energy 3607411.44 NA
CH 2015 Energy 3292576.93 NA
CH 2016 Energy 3380402.57 NA
CY 2000 Energy 2964656.86 NA
CY 2001 Energy 2847105.45 NA
CY 2002 Energy 3008827.44 NA
CY 2003 Energy 3235739.95 NA
CY 2004 Energy 3294769.3 NA
CY 2005 Energy 3483623.91 3471844
CY 2006 Energy 3665461.17 3653380
CY 2007 Energy 3814469.11 3801667
CY 2008 Energy 3980439.76 3967293
CY 2009 Energy 4005649.27 3992467
CY 2010 Energy 3880758.22 3868001
CY 2011 Energy 3722369.39 3728038
CY 2012 Energy 3557560.24 3545929
CY 2013 Energy 2839148.88 2829732
CY 2014 Energy 2950111.64 2940320
CY 2015 Energy 3032961.55 3023003
CY 2016 Energy 3310941.55 3300001
CY 2017 Energy NA 3287834")