有关DataGrid自动创建列的问题:
  (1)如何设定自动创建列的某一列为隐藏;
  (2)自动创建列的排序;
此问题已困扰我好几天了,请高人们指教!在线等·

解决方案 »

  1.   

    http://singlepine.cnblogs.com/articles/323764.html
      

  2.   

    谢谢!我回去测试一下!
    还有我在别的地方查看到以下方法,不知可不可行,一起试!DataGridTableStyle   myts=   new   DataGridTableStyle(true);
                            
    myts.MappingName=myds.Tables[0].TableName;this.dataGrid1.TableStyles.Clear();
    this.dataGrid1.TableStyles.Add(myts);
    //将第一列宽度设置隐藏
    myts.GridColumnStyles["学号"].Width=0;
      

  3.   

    CS 还是 BS哦,楼上的是C/S方法可行
      

  4.   

    谢谢小山的正解,不过我还有一个疑问,如何同步实现即可隐藏指定列又可排序的功能!
    我在你提供的方法中添加实现排序代码,结果DataGrid绑出的数据列加倍,试了好几次,
    都没能解决,请你继续指教!
    string sql="SELECT * FROM dbo.ESD_TB_Info";
    DataSet ds = GetDataSet(sql);
    if ( this.showColumnsFilter == null || this.showColumnsFilter.Trim() == string.Empty)
    {
       cols = new string[ds.Tables[0].Columns.Count];
       for(int i=0;i<ds.Tables[0].Columns.Count;i++)
       {
         cols[i]=ds.Tables[0].Columns[i].ColumnName;
       }
    }
    else
    {
       cols = this.showColumnsFilter.TrimEnd(new char[]{','}).Split(new char[]{','});
    }
    for(int i=0;i<cols.Length ;i++)
    {
       BoundColumn bc =new BoundColumn();
       bc.DataField = cols[i].ToString();
       bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
       bc.HeaderText = cols[i].ToString().Replace(" ","&nbsp;");
       this.DataGrid1.Columns.Add(bc); //
    }
    DataView dv = new DataView();
    dv=ds.Tables[0].DefaultView;
    string SortExpression=this.DataGrid1.Attributes["SortExpression"];
    string SortDirection=this.DataGrid1.Attributes["SortDirection"];
    dv.Sort=SortExpression + " " + SortDirection;
    this.DataGrid1.DataSource = dv ;
    this.DataGrid1.DataBind();
    在线等!谢谢!