基本上我不能从单元格获取值,列只包含整数,使用Convert会产生异常,因此添加fila.Cells[4].Value也是如此,我一直在尝试一些我在网上找到的解决方案(在代码中注释),但仍是如此。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow fila in Tabla.Rows)
{
//int celda = Convert.ToInt32(Tabla.Rows[4].ToString()); //ArgumentOutOfRangeException
//var celda = Convert.ToInt32(fila.Cells[4].ToString()); //FormatException
//var celda = Convert.ToInt32(fila.Cells[4]); //InvalidCastException
if (celda > 10)
{
//Visual Alert on cell
}
}
}在if语句中,它应该显示一个警告(缺货、低库存等)
有没有可能做到这一点,或者我只是在浪费时间?
发布于 2016-11-02 23:32:49
如果使用e.Row.Cells[i].Text检查单元格数据,可能会遇到问题。如果将单元格格式化为这样的<%# string.Format("{0:c}", 35000) %>,将其转换回整数35000将导致Input string was not in a correct format错误,因为它已格式化为€ 35.000,00形式的字符串。DateTime值也是如此。
一种更好的方法是将GridViewRowEventArgs转换回原始格式,并对其进行比较。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//if the bound data is a generic list, cast back to an individual listitem
Book book = e.Row.DataItem as Book;
if (book.id > 3)
{
//add an attribute to the row
e.Row.Attributes.Add("style", "background-color: red");
//or hide the entire row
e.Row.Visible = false;
}
//if the bound data is a datatable or sql source, cast back as datarowview
DataRowView row = e.Row.DataItem as DataRowView;
if (Convert.ToInt32(row["id"]) > 4)
{
//add an attribute to a cell in the row
e.Row.Cells[1].Attributes.Add("style", "background-color: red");
//or replace the contents of the cell
e.Row.Cells[1].Text = "SOLD OUT";
}
}
}https://stackoverflow.com/questions/40370548
复制相似问题