GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
效果图:
解决方案:主要是绑定后过滤
GridView1.DataBind();
for (int i = 0; i <= GridView1.Rows.Count - 1;
i++)
{
DataRowView mydrv =
myds.Tables["飞狐工作室"].DefaultView[i];
string score =
Convert.ToString(mydrv["起薪"]);
if (Convert.ToDouble(score) <
34297.00)//大家这里根据具体情况设置可能ToInt32等等
{
GridView1.Rows[i].Cells[4].BackColor =
System.Drawing.Color.Red;
}
}
sqlcon.Close();
全部后台代码:
using System;
using System.Data;
using
System.Configuration;
using System.Web;
using
System.Web.Security;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using
System.Drawing;
public partial class Default7 :
System.Web.UI.Page
{
SqlConnection
sqlcon;
SqlCommand sqlcom;
string strCon = "Data
Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
protected
void Page_Load(object sender, EventArgs e)
{
if
(!IsPostBack)
{
bind();
}
}
protected
void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex =
e.NewEditIndex;
bind();
}
protected
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon
= new SqlConnection(strCon);
string sqlstr = "update 飞狐工作室 set
姓名=‘"
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
+
"‘,家庭住址=‘"
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()
+ "‘ where
身份证号码=‘"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() +
"‘";
sqlcom
= new SqlCommand(sqlstr,
sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected
void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex =
-1;
bind();
}
public
void bind()
{
string sqlstr
= "select top 10 * from
飞狐工作室";
sqlcon = new
SqlConnection(strCon);
SqlDataAdapter myda
= new SqlDataAdapter(sqlstr,
sqlcon);
DataSet myds
= new DataSet();
sqlcon.Open();
myda.Fill(myds,
"飞狐工作室");
GridView1.DataSource
= myds;
GridView1.DataKeyNames
= new string[] { "身份证号码" };
GridView1.DataBind();
for
(int i = 0; i <= GridView1.Rows.Count - 1;
i++)
{
DataRowView mydrv =
myds.Tables["飞狐工作室"].DefaultView[i];
string score =
Convert.ToString(mydrv["起薪"]);
if (Convert.ToDouble(score) <34297.00)//大家这里根据具体情况设置可能ToInt32等等
{
GridView1.Rows[i].Cells[4].BackColor =
System.Drawing.Color.Red;
}
}
sqlcon.Close();
}
}
前台代码:
>
GridView突出显示某一单元格
>
runat="server">
>
AutoGenerateColumns="False" CellPadding="3"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White"
BorderColor="#CCCCCC" Border Border
Font-Size="12px"
>
/>
/>
/>
/>
/>
DataFormatString="{0:C}"
HtmlEncode="false"/>
/>
/>
/>
/>
HorizOntalAlign="Left" CssClass="ms-formlabel
DataGridFixedHeader"/>
/>