我对C#很新,所以我不擅长编码。目前,我正在使用Visual Studios Community 2015,并且主要从事设计视图工作。
我创建了一个绑定到SQL数据源的gridview,允许用户更新记录。
现在我要做的是:
我有2列。服务器和状态。我想根据给定的文本突出显示状态列。
例如:状态=已完成,应以绿色突出显示。
实现这一目标的最佳方法是什么?有人能指出我正确的方向,无论是链接指南还是帮助背后的代码。
谢谢,
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="SID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" ReadOnly="True" SortExpression="SID" />
<asp:BoundField DataField="Servers" HeaderText="Servers" SortExpression="Servers" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:it_ConnectionString2 %>" SelectCommand="SELECT * FROM [Main_Status_test]" UpdateCommand="Update [Main_Status_test] Set [Status]=@Status Where [SID]=@SID"></asp:SqlDataSource>
</form>
</body>
</html>
背后的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[3].Text == "done")
{
row.BackColor = System.Drawing.Color.Green;
}
else if (row.Cells[3].Text == "not done")
{
row.BackColor = System.Drawing.Color.Red;
}
}
}
答案 0 :(得分:0)
只需将以下编码添加到GridView1 DataBound事件即可。
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[2].Text == "done")
{
row.BackColor = System.Drawing.Color.Green;
}
else if (row.Cells[2].Text == "WHATEVER YOU WANT")
{
row.BackColor = System.Drawing.Color.'Whatevercoloryouwant'
}
//etc...
}