读书人

gridview列判断出错死人了。该怎么处

发布时间: 2012-02-24 16:30:38 作者: rapoo

gridview列判断出错,急死人了。
首先Cell[4]和Cell[8]两列的绑定的数据都是不含null的float型字段,其中Cell[8]还是公式字段(即在SQLSERVER中设置公式),并且在GridView该列设置只读,只有Cell[4]是可编辑的.我想当Cell[4]输入某值时通过判断Cell[8]的值大于Cell[4]*0.15时显示红色粗体.

现在非常奇怪的问题出现了.
1、 以下代码是放在Page_Load中的,不知是否正确?
2、当随意输入数值(当然不超过float范围)的时候,catch到的ex提示“输入无效”,而我使用showMsg(GridView1.Rows[i].Cells[4].Text);每行输出时却真的是提示刚输入的是空值!!!,但如果不点击编辑这样却每行的Cell[4]都为数值,就是当编辑时输入时却是空值。搞到我头大了。


if (!IsPostBack)
{
try
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
if (Convert.ToDouble(GridView1.Rows[i].Cells[8].Text) > Convert.ToDouble(GridView1.Rows[i].Cells[4].Text) * 0.15)
{
GridView1.Rows[i].Cells[8].ControlStyle.ForeColor = System.Drawing.Color.Red;
GridView1.Rows[i].Cells[8].ControlStyle.Font.Bold = true;
showMsg(GridView1.Rows[i].Cells[4].Text);
//GridView1.DataBind();
}
}
}
catch (Exception ex)
{
showMsg(ex.Message);
}
}

请大家帮帮忙!

[解决办法]
曾经遇到过产这样的情况,不知能否给你一点提示
在编辑CurrentCell时,是不能立即获取正在编辑CurrentCell的值,只有当CurrentCell改变时,才能获取刚编辑的Cell的值
在编辑状态要想获取CurrentCell的值,要先改变CurrentCell,再对Cell获取值
------解决方案--------------------


DataRowView dr = (DataRowView)e.Row.DataItem;
if (dr[ "PaidOffDate "] != DBNull.Value)
{
e.Row.Attributes.Add( "style ", "background-color:#99CC23; ");
}
这个可以判断非编辑行的值。
如果要取编辑行的值建议你转换成模板列然后FindControl
[解决办法]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (dr[ "YingFa "] != DBNull.Value)
{
if (Convert.ToDouble(dr[ "YuLiu_T "]) > Convert.ToDouble(dr[ "YingFa "]) * 0.15)
{
e.Row.Cells[8].ControlStyle.ForeColor = System.Drawing.Color.Red;
e.Row.Cells[8].ControlStyle.Font.Bold = true;
}
}
}
}

[解决办法]
关键是代码的位置不应该在Page_load里而是象上面的这位写的那样,放到bound事件中去
[解决办法]
1. 不知道你用的是不是模版列,模版列控制起来相对容易
2. 看你现在的问题,似乎用javascript更好写,否则很难有相对直观的显示

读书人网 >asp.net

热点推荐