我要将表中的数据都读到DataGrid中,但列数太多了,我就在最后三列的HeaderTemplate中分别添加了三个下拉菜单,想实现这三列的内容由下拉菜单里面用户选择的列名决定。
怎样做哦????

解决方案 »

  1.   

    DropDownList xx=new DataGrid()
    PlaceHolder1.Controls.Add(xx);用一个PlaceHolder看可以不可以,呵呵!
      

  2.   

    写错了呵呵
    DropDownList xx=new DropDownList()
    PlaceHolder1.Controls.Add(xx);
      

  3.   

    用visible属性,或者实时添加绑定列
      

  4.   

    比如要添加一个BoundField,就先实例化,再绑字段,最后加入DataGrid。我是在GridView中绑定的,想来DataGrid差不多。
    BoundField bf = new BoundField();
    bf.DataField = "数据库中的字段名";
    bf.DataFormatString = "{0:d}"; //字段显示格式,这里表示短日期
    bf.HtmlEncode = false;  //用到格式化字符串的话,就要把这个设为false
    this.viewList.Columns.Add(bf);   //viewList就是GridView网络控件
      

  5.   

    我现在的做法是:先把显示不下的列都隐藏起来,并且每个列的HeaderTemplate中都有一个选项都相同的的DropDownList,有用户选择要显示的列。
    DropDownList的SelectedItem.Value设置为列的序号。
    我用下面的方法,但选择DropDownList中的项时,不管DropDownList的AutoPostBack设为true还是false,没有反应.
    private void ShowCol(DropDownList dp, DataGrid dg, int index)
    {
    if(Int32.Parse(dp.SelectedItem.Value) != index)
    {
    dg.Columns[Int32.Parse(dp.SelectedItem.Value)].Visible = true;
    dg.Columns[index].Visible = false;
    }
    }DropDownList[] dps;private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Header)
    {
    DropDownList dp1 = (DropDownList)e.Item.FindControl( "dp1" );
    DropDownList dp2 = (DropDownList)e.Item.FindControl( "dp2" );
    DropDownList dp3 = (DropDownList)e.Item.FindControl( "dp3" );
    DropDownList dp4 = (DropDownList)e.Item.FindControl( "dp4" );
    DropDownList dp5 = (DropDownList)e.Item.FindControl( "dp5" );
    DropDownList dp6 = (DropDownList)e.Item.FindControl( "dp6" );
    DropDownList dp7 = (DropDownList)e.Item.FindControl( "dp7" );
    DropDownList dp8 = (DropDownList)e.Item.FindControl( "dp8" );
    dps = new DropDownList[8]{dp1, dp2,dp3,dp4, dp5, dp6, dp7, dp8};
    for(int i = 1; i <= 8; i++)
    {
        this.ShowCol(dps[i - 1], this.DataGrid1, i+2);
    }
        }
    }
    -----------------
    请各位想想办法。你们也做做,不要凭空乱造哈,真的会害死人的哦!!!
    谢谢!
      

  6.   

    你程序的思路还是可行的.只不过我觉得你在调用showcol函数这里出了点问题.自己再想想吧