最近做一个WAP的查询系统,要求默认显示3列主要信息,其余信息可以通过按钮显示所有列实现,通过以下方法显示指定列信息。

按钮的事件 

protected void Button2_Click(object sender, EventArgs e)
        {
            if (GridView1.Columns.Count == 3)
            {
                Button2.Text = "默认显示";
                GridView1.Columns.Clear();
                //给字段加别名
                AddGridViewColumn("BU", "BU");
                AddGridViewColumn("SU", "SU");
                AddGridViewColumn("大区", "Region");
                AddGridViewColumn("地区", "District");
                AddGridViewColumn("员工工号", "EmployeeCode");
                AddGridViewColumn("姓名", "EmployeeName");
                AddGridViewColumn("奖项", "Award");
                AddGridViewColumn("明星类型", "StarType");
                AddGridViewColumn("评选分组", "StarGroup");
                AddGridViewColumn("产品", "Product");
                AddGridViewColumn("季度", "Quarter");
            }
            else
            {
                Button2.Text = "显示所有列";
                GridView1.Columns.Clear();
                //给字段加别名             
                AddGridViewColumn("地区", "District");            
                AddGridViewColumn("姓名", "EmployeeName");             
                AddGridViewColumn("明星类型", "StarType");              
            }
            this.Search();
        }

加别名

  /// <summary>
        /// 给gridView加别名字段
        /// </summary>
        /// <param name="title"></param>
        /// <param name="value"></param>
        protected void AddGridViewColumn(string title, string value)
        {
            BoundField col = new BoundField();
            col.HeaderText = title;
            col.DataField = value;
            col.Visible = true;
            GridView1.Columns.Add(col);
        }