在GridView中如何让符合条件的数据字体变色;有一点难度。 gridview 列A1(int),A2,A3,A4,A5,A6 后面的是是varchar
在gridview里显示如下;
a1 a2(名称) a3(检查结果) a4(数值型下限) a5(数值型上限) a6(字符型标准)
1 x1 营养不良 良好
1 x2 肥胖 正常
1 x3 10 8 20
1 x4 50 30 40
2 x1 良好 良好
2 x2 正常 正常
2 c1 80 70 100
2 c2 30 10 20
2 c3 45 20 40
............(略)
想要得到结果在gridview里
a3列的内容是 数值型的数据 用 a4 和a5 判断 如果不在两者之间就显示红色 例;x4的50 不在 30-40之间 就显示红色
c2和c3 也同样;(我用了好多方法都不能达到要求,各位同仁有没有更好的方法)能回答上面的也给满分!!!, 能顺便把下面的回答了更好。a3(检查结果) 是汉字的 比对 a6(字符型标准) 如果不同就显示红色 如; 营养不良--和良好不匹配 显示红色; 肥胖--正常不匹配 显示红色
用sql语句也是可以的但是太麻烦了。长期在线 测试过马上给分!!
在gridview里显示如下;
a1 a2(名称) a3(检查结果) a4(数值型下限) a5(数值型上限) a6(字符型标准)
1 x1 营养不良 良好
1 x2 肥胖 正常
1 x3 10 8 20
1 x4 50 30 40
2 x1 良好 良好
2 x2 正常 正常
2 c1 80 70 100
2 c2 30 10 20
2 c3 45 20 40
............(略)
想要得到结果在gridview里
a3列的内容是 数值型的数据 用 a4 和a5 判断 如果不在两者之间就显示红色 例;x4的50 不在 30-40之间 就显示红色
c2和c3 也同样;(我用了好多方法都不能达到要求,各位同仁有没有更好的方法)能回答上面的也给满分!!!, 能顺便把下面的回答了更好。a3(检查结果) 是汉字的 比对 a6(字符型标准) 如果不同就显示红色 如; 营养不良--和良好不匹配 显示红色; 肥胖--正常不匹配 显示红色
用sql语句也是可以的但是太麻烦了。长期在线 测试过马上给分!!
解决方案 »
- 关于表单提交
- 提供.net环境下的加密保护方案
- 如何把“答案”字段为空值的替换为”M”?
- RequiredFieldValidator控件引起事件无效的问题
- 高手请进“ASP.NET怎么将Word文档转换成HTML文件?”
- 从网上下载一个asp.net源码,传到服务器里面,应该注意些什么呢?
- TreeView高手请进(查询数据库生成Xml文件,再绑定到TreeView控件上)在线等!!!
- 100分求一个很烦恼的问题:如何让datalist在设计完后显示时总是四行,不能让它是一行吗?
- dataview 可以使用 sum("aa") 吗?
- 寻求语句高手,寻求语句高手!!!!!!!!!
- 求助:用户配置方件问题
- Membership默认的machineKey哪里来的?
a1 a2(名称) a3(检查结果) a4(数值型下限) a5(数值型上限) a6(字符型标准)
1 x1 营养不良 良好
1 x2 肥胖 正常
1 x3 10 8 20
1 x4 50 30 40
2 x1 良好 良好
2 x2 正常 正常
2 c1 80 70 100
2 c2 30 10 20
2 c3 45 20 40
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string result = e.Row.Cells[2].Text;
int temp;
if (int.TryParse(result,out temp))
{
if (Convert.ToInt32(e.Row.Cells[2].Text) < Convert.ToInt32(e.Row.Cells[3].Text)
|| Convert.ToInt32(e.Row.Cells[2].Text) > Convert.ToInt32(e.Row.Cells[4].Text))
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
else
{
if (e.Row.Cells[2].Text != e.Row.Cells[4].Text)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
}
http://www.codeproject.com/aspnet/GridThemes.asp
protected string BoundInfo(string str1,string str2,string str3,string str4)
{
if (Convert.ToInt32(str1)< Convert.ToInt32(str2)
¦ ¦ Convert.ToInt32(str3) > Convert.ToInt32(str4))
{
return <font color=red>str1</font>;
} }
{
if (e.Row.Cells[8].Text == "USA")
{
//e.Row.BackColor = System.Drawing.Color.Red;
e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
}类似的你改一下就行了
第三列(e.Row.Cells[2].Text)和第四列(e.Row.Cells[4].Text)为空的时候,
这里的三、四列的值是   然后就报错类型错误。 可是空的 我又不能跳过去,我再测试
Convert.ToInt32(e.Row.Cells[2].Text) < Convert.ToInt32(e.Row.Cells[3].Text)
||Convert.ToInt32(e.Row.Cells[2].Text) > Convert.ToInt32(e.Row.Cells[4].Text)
例如 三列是 5.8 三列是3,四列是10这个也是红色因为是text类型的吧
我这里有小数点的列还挺多,大部分正常值的范围内也变成红色。这个问题你能给我个建议吗?
例如 三列(三列就a3也就是e.Row.Cells[2].Text)是 5.8 四列是3,5列是10这个也是红色因为是text类型的吧
这里的第四列(e.Row.Cells[3].Text)和第5列(e.Row.Cells[4].Text)
我这里有小数点的列还挺多,大部分正常值的范围内也变成红色。这个问题你能给我个建议吗?
把转换的那段改成下面就可以了,其实如果你给的数据有小数点的话
我早就写成这样了:)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string result = e.Row.Cells[2].Text;
double temp;
if (Double.TryParse(result,out temp))
{
if (Convert.ToDouble(e.Row.Cells[2].Text) < Convert.ToDouble(e.Row.Cells[3].Text)
|| Convert.ToDouble(e.Row.Cells[2].Text) > Convert.ToDouble(e.Row.Cells[4].Text))
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
else
{
if (e.Row.Cells[2].Text != e.Row.Cells[4].Text)
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
}
{
string conn = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("./data/english.mdb");
string cmd = "select id,title,type,dt from News";
OleDbDataAdapter da = new OleDbDataAdapter(cmd, conn);
DataSet ds = new DataSet();
da.Fill(ds, "News");
GridView1.DataSource = ds;
//设置主键;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
//解决方案:主要是绑定后过滤
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv = ds.Tables["News"].DefaultView[i];
string Temp = mydrv["type"].ToString();
if (Temp == "娱乐新闻") //大家这里根据具体情况设置可能ToInt32等等
{
GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Red; //这里要修改单元格位置;
}
} } protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
如e.Row.Cells[2].Text.Trim()
以免不规则数据的干扰
if (e.Row.RowType == DataControlRowType.DataRow)
{
string result = e.Row.Cells[2].Text.Trim ();
double temp;
if (Double.TryParse(result,out temp))
{
if (Convert.ToDouble(e.Row.Cells[2].Text.Trim ()) < Convert.ToDouble(e.Row.Cells[3].Text.Trim ())
||Convert.ToDouble(e.Row.Cells[2].Text.Trim ()) > Convert.ToDouble(e.Row.Cells[4].Text.Trim ()))
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
else
{
if (e.Row.Cells[2].Text.Trim () != e.Row.Cells[5].Text.Trim ())
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
我再换换float测试一下,我们是不是该想的地方都想到了,问题就有点奇怪了 呵呵
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text != " " || e.Row.Cells[4].Text != " " || e.Row.Cells[5].Text != " ")
{
string result = e.Row.Cells[2].Text.Trim ();
double temp;
if (Double.TryParse(result,out temp))
{
if (Convert.ToDouble(e.Row.Cells[2].Text.Trim ()) < Convert.ToDouble(e.Row.Cells[3].Text.Trim ())
||Convert.ToDouble(e.Row.Cells[2].Text.Trim ()) > Convert.ToDouble(e.Row.Cells[4].Text.Trim ()))
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
else
{
if (e.Row.Cells[2].Text.Trim () != e.Row.Cells[5].Text.Trim ())
{
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
}
}
}
}
}你最后一个判断的是第6行。
重要的是你上面提到的   是非间断空值 不能完全理解为空格,也不是能判断为空。否则报错;