实现gridview的动态列绑定

学习asp.net有一段时间了,但感觉对他就不了解,似懂非懂,这不要求动态绑定gridview列把我难住了,经过研究,终于实现所要的效果。
需求:gridview绑定一个试图,但是此试图也是用存储过程和触发器实现,也就是说其列是动态的(有几列是固定的,其他不固定),有两个dropdownlist它绑定数据源,绑定的字段在上面的试图中也出现,要求dropdownlist的selectvalue值变而gridview列也同样变。
实行:思想也很简单,添加几个sqldatasource,根据不同的需求绑定不同的sqldatasource,每次绑定时删除已绑定的列,添加需求的列。具体实现如下:
 protected void Button1_Click(object sender, EventArgs e)//button事件
    {
        string i="none";
        if (this.DropDownList1.SelectedValue != "请选择" && this.TypeDrp.SelectedValue != "请选择")//判断是否都选中
            i ="all";          
        if (this.DropDownList1.SelectedValue != "请选择" && this.TypeDrp.SelectedValue == "请选择")//判断按工时类型查询
            i = "time";
        if (this.DropDownList1.SelectedValue == "请选择" && this.TypeDrp.SelectedValue != "请选择")//判断按类型查询
            i = "class";
        if (this.DropDownList1.SelectedValue == "请选择" && this.TypeDrp.SelectedValue == "请选择")//都不选择,查询全部
            i = "none";
        switch (i)
        {
            case "all":
                {
                    int n = this.GridView1.Columns.Count;//先删除gridview绑定所有列

                    for (int m = 0; m < n; m++)
                    {
                        this.GridView1.Columns.RemoveAt(0);
                    }
                    //重新定义sqldatasource的selectCommand字段值,主要是实现查询非空列
                    this.SqlDataSource2.SelectCommand="SELECT WH_工时定额旋转视图.*, WH_工时类型.* FROM WH_工时定额旋转视图 CROSS JOIN WH_工时类型 WHERE (WH_工时定额旋转视图.分类 = @分类) AND (WH_工时类型.工时类型 = @工时类型) and "+this.DropDownList1.SelectedValue.ToString()+" is not null";
                    this.GridView1.DataSourceID = "SqlDataSource2";
                   
                    this.GridView1.AutoGenerateColumns = false;//不自动填充绑定列
                    this.GridView1.DataBind();//绑定
                   
                    if (x>0)//如何sqldatasource2的记录行数>0,就添加列(按需添加)
                    {
                        InsertColumns(this.DropDownList1);
                        x = 0;//重置x=0
                    }
                    else//记录行数为0,弹出提示
                    {
                        Response.Write("<script language='javascript' type='text/javascript'>");
                        Response.Write("alert('Not Find!')");
                        Response.Write("</script>");
                    }
                    break;
                }
            case "time"://同上
                {
                    int n = this.GridView1.Columns.Count;
                    Response.Write(n.ToString());
                    for (int m = 0; m < n; m++)
                    {
                        this.GridView1.Columns.RemoveAt(0);
                    }
                    this.GridView1.DataSourceID = "SqlDataSource3";
                    this.SqlDataSource3.SelectCommand = "SELECT WH_工时类型.*, WH_工时定额旋转视图.* FROM WH_工时类型 CROSS JOIN WH_工时定额旋转视图 where 工时类型=@工时类型 and " + this.DropDownList1.SelectedValue.ToString() + " is not null";
                    this.GridView1.AutoGenerateColumns = false;
                  
                    this.GridView1.DataBind();
                    if (y > 0)
                    {
                        InsertColumns(this.DropDownList1);
                        y = 0;
                    }
                    else
                    {
                        Response.Write("<script language='javascript' type='text/javascript'>");
                        Response.Write("alert('Not Find!')");
                        Response.Write("</script>");
                    }
                    break;
 
                }
            case "class"://同上
                {
                    int n = this.GridView1.Columns.Count;

                    for (int m = 0; m < n; m++)
                    {
                        this.GridView1.Columns.RemoveAt(0);
                    }
                    this.GridView1.DataSourceID = "SqlDataSource4";
                    this.GridView1.AutoGenerateColumns =true;
                    this.GridView1.DataBind();
                    break;
 
                }
            default:
                {
                    int n = this.GridView1.Columns.Count;

                    for (int m = 0; m < n; m++)
                    {
                        this.GridView1.Columns.RemoveAt(0);
                    }
                    this.GridView1.DataSourceID = "SqlDataSource1";
                    this.GridView1.AutoGenerateColumns = true;
                    this.GridView1.DataBind();
                    break;
 
                }
        }

    }
private void Insert4Columns()//添加4列,因为这四列都是必须显示的
    {
        string[] title=new string[4];
        title[0] = "公司编号";
        title[1] = "作业项目";
        title[2] = "分类";
        title[3] = "单位";

        for (int m = 0; m < 4; m++)
        {
            BoundField ds = new BoundField();//定义一个BoundField,用于添加到gridview列中
            ds.DataField = title[m];
            ds.HeaderText = title[m];
            this.GridView1.Columns.Add(ds);
 
        }
       
    }
    private void InsertColumns(DropDownList dl)//根据dropdownlist添加列,主要根据需求
    {
       string dd= dl.SelectedValue.ToString();
       BoundField ds = new BoundField();     
       ds.DataField = dd;
       ds.HeaderText = dd;
       BoundField dss = new BoundField();
       dss.DataField = dd+"加";
       dss.HeaderText = dd+"加";
      
           Insert4Columns();
           this.GridView1.Columns.Add(ds);
           this.GridView1.Columns.Add(dss);
     
 
    }

    protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        x = e.AffectedRows;//前面定义成static,返回sqlDataSource2的记录行数
    }
    protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        y = e.AffectedRows;//前面定义成static,返回sqlDataSource3的记录行数
    }
    protected void SqlDataSource4_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        z = e.AffectedRows;//前面定义成static,返回sqlDataSource4的记录行数
    }

posted @ 2007-04-14 21:58  wasp  阅读(4656)  评论(0)    收藏  举报