ASP2.0 GridView 隐藏列或显示列的方法

protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  //隐藏不必要的列
  if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer))
  {
  e.Row.Cells[0].Visible = false;
  e.Row.Cells[3].Visible = false;
  }
  }

  这是迄今为止最简洁的解决方法了。

在RowCreated事件中书写如下代码:

  void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow ||
  e.Row.RowType == DataControlRowType.Header)
  {
  e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
  }
  //可以根据需要设置更多的列
  }
因为在RowCreated事件(隐藏)在绑定时候发生,所以这样就即能将数据绑定到列上,又隐藏了该列.所以可以访问到隐藏列的值。
隐藏显示列
============================

// 全选
         protected   void  CB_selALL_CheckedChanged( object  sender, EventArgs e)
        
{
            BLL.InitialGridViewClass objInitialGridViewClass 
= new BLL.InitialGridViewClass();
            objInitialGridViewClass.AllSel(CB_selALL, gv, 
"CheckBox1");


            
if (CB_selALL.Checked)
            
{
                gv.Columns[
6].Visible = true;
                gv.Columns[
7].Visible = true;
                gv.Columns[
8].Visible = true;
            }

            
else
            
{
                gv.Columns[
6].Visible = false;
                gv.Columns[
7].Visible = false;
                gv.Columns[
8].Visible = false;
            }

        }

// --------------------
public   void  AllSel(CheckBox objCBAll, GridView GV,  string  strCB)
        
{
            
for (int i = 0; i < GV.Rows.Count; i++)
            
{
                CheckBox objCB 
= (CheckBox)GV.Rows[i].FindControl(strCB);
                
if (objCBAll.Checked)
                
{
                    objCB.Checked 
= true;
                }

                
else
                
{
                    objCB.Checked 
= false;
                }

            }

        }

// =========将GridView 中的CheckBox打沟 后显示列,去掉沟则隐藏列=========
// 打沟 后的变化
         protected   void  CheckBox_Changed( object  sender, EventArgs e)
        
{
            
int n = 0;
            
for (int i = 0; i < gv.Rows.Count; i++)
            
{
                CheckBox objCB 
= (CheckBox)gv.Rows[i].FindControl("CheckBox1");
                
if (objCB.Checked)
                    n
++;
           }

            
if (n > 0)
            
{
                gv.Columns[
6].Visible = true;
                gv.Columns[
7].Visible = true;
                gv.Columns[
8].Visible = true;
            }

            
if (n == 0)
            
{
                gv.Columns[
6].Visible = false;
                gv.Columns[
7].Visible = false;
                gv.Columns[
8].Visible = false;
            }

        }

你可能感兴趣的:(GridView)