首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否从GridView中的单元格获取INT值以进行库存控制?

是否从GridView中的单元格获取INT值以进行库存控制?
EN

Stack Overflow用户
提问于 2016-11-02 08:13:28
回答 2查看 1.9K关注 0票数 0

基本上我不能从单元格获取值,列只包含整数,使用Convert会产生异常,因此添加fila.Cells[4].Value也是如此,我一直在尝试一些我在网上找到的解决方案(在代码中注释),但仍是如此。

代码语言:javascript
运行
复制
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语句中,它应该显示一个警告(缺货、低库存等)

有没有可能做到这一点,或者我只是在浪费时间?

EN

Stack Overflow用户

发布于 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转换回原始格式,并对其进行比较。

代码语言:javascript
运行
复制
    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";
            }
        }
    }
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40370548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档