嗯,你没有说你打算从哪里得到这些颜色?
我的意思是,你的颜色需要有一些逻辑。但是,一旦你有了一些设置颜色的方法,那么解决方案就相当简单了。
比如说,我有一张酒店表格。
对于 City = Jasper,我想要绿色,对于 Banff,我想要蓝色。
所以,说这个网格视图:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
以及加载上述网格的代码?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGrid();
}
}
void LoadGrid(string strCity = "")
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * FROM tblHotels",
new SqlConnection(Properties.Settings.Default.TEST3)))
{
cmdSQL.Connection.Open();
GridView1.DataSource = cmdSQL.ExecuteReader();
GridView1.DataBind();
}
}
因此输出是这样的:
现在,要上色吗?那么我们可以使用 RowDataBound 事件,像这样:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
switch (e.Row.Cells[3].Text)
{
case "Jasper":
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Blue;
break;
}
case "Banff":
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
break;
}
}
}
}
现在我们的输出如下所示:
但是,如果您的 gv 使用默认列或绑定字段列,您是否遗漏了 HUGE WHOPPER 详细信息?如果是,那么上面会正常工作。
如果您使用模板列,则需要使用不同的方法。
因此,假设 City 是一个标准的 say 标签,如下所示:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="txtCity" runat="server" Text= '<%# Eval("City") %>' > </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
那么我们的 RowDatabound 代码如下所示:
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label txtCity = (Label)e.Row.FindControl("txtCity");
switch (txtCity.Text)
{
case "Jasper":
{
txtCity.ForeColor = System.Drawing.Color.Blue;
break;
}
case "Banff":
{
txtCity.ForeColor = System.Drawing.Color.Green;
break;
}
}
}
最后但并非最不重要?如果我有 20 个不同的城市和 20 种不同的颜色,当然不会有人对颜色进行硬编码。
所以,我会说制作/拥有/创建/使用城市表,并说出我想要的颜色,然后将其加入查询中。在这种情况下,我会像您一样使用表绑定网格,因为绑定期间的数据源甚至可以使用数据源中的任何列。如果您像我一样使用 ExecuteReader,那么您仅限于网格中的列,而不是 gv 数据源中的其他列。 (例如在这个想法颜色中)。这意味着如果您要使用未出现在网格中的列(例如在这个想法中 - 颜色不会是 gv 显示中的列,但是它将在表数据源中,因此可以在绑定事件期间使用)。
编辑 ---------------------------------- em>
所以,代替在代码中硬编码颜色,我们可以这样做:
当然,我们使用了这里的颜色:
https://en.wikipedia.org/wiki/Web_colors
好的,现在我们将上述颜色表中的简单 LEFT 连接到我们的查询中。
请注意,我们必须使用表,因为在数据绑定期间,我需要/想要充分使用数据行 - 即使是我不想显示的列。所以在下面的标记中,我现在可以删除城市列,但仍然在数据绑定中使用城市。 (但是,嘿,让我们把它留给这篇文章吧。
所以,我的标记列现在是这样的:
<asp:TemplateField HeaderText="Color">
<ItemTemplate>
<asp:Label ID="txtColor" runat="server" Text= '<%# Eval("Color") %>' Font-Bold="true"> </asp:Label>
</ItemTemplate>
(其余同前)。
现在我的查询+数据绑定代码是这样的:
using (SqlCommand cmdSQL = new SqlCommand("SELECT *,Color FROM tblHotels "
+ "LEFT JOIN tblCityColors on tblHotels.City = tblCityColors.City "
+ "ORDER BY HotelName",
new SqlConnection(Properties.Settings.Default.TEST3)))
{
cmdSQL.Connection.Open();
DataTable rst = new DataTable();
rst.Load(cmdSQL.ExecuteReader());
GridView1.DataSource = rst;
GridView1.DataBind();
}
现在我们的 Row 数据绑定事件是这样的:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label txtCity = (Label)e.Row.FindControl("txtCity");
Label txtColor = (Label)e.Row.FindControl("txtColor");
DataRowView dr = (DataRowView)e.Row.DataItem;
txtColor.ForeColor = System.Drawing.Color.FromName(dr["Color"].ToString());
txtCity.ForeColor = System.Drawing.Color.FromName(dr["Color"].ToString());
}
}
如前所述,我们可以从上面删除城市列 - 但对于这个演示,没关系。
现在我们的输出是这样的:
请注意我们在绑定事件期间如何获取 FULL 数据行:
DataRowView dr = (DataRowView)e.Row.DataItem;
因此,GridView 数据源仅在数据绑定事件期间有效,并且仅在活动期间有效。渲染网格后,DataItem(用于行)或 GridView.DataSource 为 null 且无效 - 它仅在绑定事件期间存在,但这就是我们需要它的全部。
因此,我们可以使用 SQL 中的任何和所有列——甚至是那些未显示在网格中的列。