【问题标题】:How to check for null column in a Gridview column如何检查 Gridview 列中的空列
【发布时间】:2012-05-23 11:22:28
【问题描述】:

我在 asp.net 3.5 网页中有一个 GridView 控件,以下代码在 RowDataBound 事件中执行,它会更改背景颜色和字体颜色,一旦列中的值:RegWaitTime 和 TotalWegTime 大于 30,

该值来自 sql server 中的两个计算列,它返回从其他两列减去的结果,这里的问题是,如果这些列中的值为 NULL,我将在更改代码的代码上得到错误颜色,对不起,我的英语,如果您需要我澄清我的要求,请告诉我,

提前致谢

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {



        // Change Wait Time Cell Rows  CHECK THE LOGIC HERE

        if (e.Row.RowType == DataControlRowType.DataRow)
         {
             // This line will get the reference to the underlying row
             DataRowView _row = (DataRowView)e.Row.DataItem;
             if (_row != null)
             {
                 // get the field value which you want to compare and
                 // convert to the corresponding data type
                 // i assume the fieldName is of int type
                 int _field = Convert.ToInt32(_row.Row["RegWaitTime"]);
                 if (_field > 30)
                 {
                     e.Row.Cells[9].BackColor = System.Drawing.Color.Red;
                     e.Row.Cells[9].Style.Add("color", "white");

                 }
                 else
                     e.Row.Cells[9].BackColor = System.Drawing.Color.Green;
                     e.Row.Cells[9].Style.Add("color", "white");
             }

         }


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // This line will get the reference to the underlying row
            DataRowView _row2 = (DataRowView)e.Row.DataItem;


            if (_row2 != null)
            {
                // get the field value which you want to compare and
                // convert to the corresponding data type
                // i assume the fieldName is of int type
                int _field = Convert.ToInt32(_row2.Row["TotalRegTime"]);
                if (_field > 30)
                {
                    e.Row.Cells[10].BackColor = System.Drawing.Color.Red;
                    e.Row.Cells[10].Style.Add("color", "white");

                }
                else
                    e.Row.Cells[10].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[10].Style.Add("color", "white");
            }
        }




     }

【问题讨论】:

    标签: asp.net sql-server-2008 gridview


    【解决方案1】:

    您需要将转换代码更改为:

    int field=0;
    
    if(int.TryParse(_row.Row["RegWaitTime"],out field))
    {
        if(field>30)
        {
        }
    }
    

    您需要这个的原因是,如果_row.Row["RegWaitTime"]NULL,那么您将获得的实际值是DBNull.Value,它会因Convert.ToInt32 抛出InvalidCastException 而窒息,因为无法将对象从DBNull 转换为其他类型。

    另一种选择是:

     if (!DBNull.Value.Equals(_row.Row["RegWaitTime"]))
     {
        //then do the conversion as you are doing now. 
     }
    

    【讨论】:

      【解决方案2】:

      如下检查数据库NULL:

      if(_row.Row["RegWaitTime"] != DBNull.Value)
      {
          //change colour
      {
      else
      {
          //do what you need to do when you get a NULL value
      }    
      

      【讨论】:

        猜你喜欢
        • 2010-10-18
        • 1970-01-01
        • 2019-02-22
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 2019-02-03
        • 2011-03-10
        • 2020-03-07
        相关资源
        最近更新 更多