检查gridview中的空值

时间:2018-03-19 11:18:00

标签: c# asp.net c#-3.0

我在gridview中有2列我想检查列是否为null然后该列将被隐藏..我使用了很多方法,但我无法获得正确的结果

这是我的代码

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells[8].Text == "")
    {
        e.Row.Cells[8].Visible = false;
        e.Row.Cells[9].Visible = false;
    }
}

我还尝试检查相等的空字但是同样的问题没有隐藏的列

4 个答案:

答案 0 :(得分:0)

尝试这样的事情:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  string val = Convert.ToString(e.Row.Cells[0]);  //check first cell value
  if(string.IsNullorEmpty(val) )             
  {
     gv.Columns[0].Visible = false;     //Hides First Column
  }
}

答案 1 :(得分:0)

试试这个:

if(!Convert.IsDBNull(GridView1.Rows[0].Cells[8].Value))
{
    GridView1.Rows[0].Cells[8].Visible = false;
}       

答案 2 :(得分:0)

尝试更改查询,例如:

select id,name,ISNULL(rate,0) from example
else  use IsNull(rate,'')

您必须像这样更改并将查询放入Bind()函数

答案 3 :(得分:0)

不确定可以尝试这种方式:

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)
             {
               GridView1.Columns[0].Visible = false;
}
}