http://www.yzcc.com/2004/12-15/15215286924.html
就是这个,我按照它说的尝试着做,但是没有成功...那位大侠实现了的,请给一个完整代码参考 谢谢!!

解决方案 »

  1.   

    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace WebApplication2
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Panel Panel1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2;

    private void Page_Load(object sender, System.EventArgs e)
    {   
    creategrid();
            FillColumnsList(DataGrid1);
      
    }      private void databind()
    {string  myconn="server=(local);uid=sa;database=pubs";
    SqlConnection  conn = new   SqlConnection(myconn);
    string str= "select * from authors";
    SqlDataAdapter command = new SqlDataAdapter(str,conn);
    DataSet ds =new DataSet();
    command.Fill(ds);
    DataGrid1.DataSource=ds;
                DataGrid1.DataBind();
    } private void creategrid()
    {      BoundColumn col =new BoundColumn();
    col.HeaderText="1";
    col.DataField="au_lname";
    DataGrid1.Columns.Add(col); col =new BoundColumn();
    col.HeaderText="2";
    col.DataField="au_fname";
    DataGrid1.Columns.Add(col); col =new BoundColumn();
    col.HeaderText="3";
    col.DataField="address";
    DataGrid1.Columns.Add(col);             databind(); } private void FillColumnsList(DataGrid grid)
    {
    foreach (DataGridColumn col in grid.Columns)
    {
    DropDownList1.Items.Add(col.HeaderText);
    }

    private void HideDataGridColumn(int index, bool show)
    {
    DataGrid1.Columns[index].Visible = show; 
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {

    HideDataGridColumn(DropDownList1.SelectedIndex, false); } private void Button2_Click(object sender, System.EventArgs e)
    {
        HideDataGridColumn(DropDownList1.SelectedIndex, true);
    }
    }
    }
      

  2.   

    有个回发的问题我没有解决 ?不知道谁能帮我看看,就是DROPDOWNLIST …………。大家测试一写一就知道了 写的很烂 呵呵
      

  3.   

    不对啊...  这样的话,隐藏第一列之后,如果还像继续隐藏第二列的话,第一列就显示出来了啊... 这样实际上只能隐藏一列!!!DropDownList的绑定可以就在写在if(!Page.IsPostBack){}里面,这样就绑定一次了...
      

  4.   

    我改了一下,这下基本可以实现... 但是我觉得有些地方写得不好... 不知道不用Session可以不?! 请继续讨论
    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">    void Page_Load(Object Sender,EventArgs E)
        {        creategrid();
            if(!Page.IsPostBack)
            {
                Session["colArr"]=new int[3]{1,1,1};
                FillColumnsList(dgCustomize);
            }
        }    private void databind()
        {
            string  myconn="server=(local);trusted_connection=yes;database=pubs";
            SqlConnection  conn = new   SqlConnection(myconn);
         string str= "select * from authors";
         SqlDataAdapter command = new SqlDataAdapter(str,conn);
         DataSet ds =new DataSet();
         command.Fill(ds);
         dgCustomize.DataSource=ds;
            dgCustomize.DataBind();
        }    private void creategrid()
        {
            BoundColumn col =new BoundColumn();
         col.HeaderText="1";
         col.DataField="au_lname";
         dgCustomize.Columns.Add(col);     col =new BoundColumn();
         col.HeaderText="2";
         col.DataField="au_fname";
         dgCustomize.Columns.Add(col);     col =new BoundColumn();
         col.HeaderText="3";
         col.DataField="address";
         dgCustomize.Columns.Add(col);        databind();
        }    private void FillColumnsList(DataGrid grid)
        {
         foreach (DataGridColumn col in grid.Columns)
         {
         ddlColSelect.Items.Add(col.HeaderText);
         }
        }    private void ColVisible(int index, bool show)
        {
            int[] colShow = new int[3];
            colShow = (int[])Session["colArr"];
            if(show)
                colShow[index]=1;
            else
                colShow[index]=0;
            Session["colShow"]=colShow;
            for(int i=0;i<3;i++)
            {
                dgCustomize.Columns[i].Visible = Show(colShow[i]);
                Response.Write(colShow[i]);
            }
        }    private bool Show(int i)
        {
            if(i==1)
                return true;
            else
                return false;
        }    private void btnHide_Click(Object Sender, EventArgs E)
        {
            ColVisible(ddlColSelect.SelectedIndex,false);
        }    void btnShow_Click(object sender, EventArgs e)
        {
            ColVisible(ddlColSelect.SelectedIndex,true);
        }</script>
    <html>
    <head>
        <link href="style.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <form runat="server">
            <p>
                <asp:datagrid id="dgCustomize" runat="server" AutoGenerateColumns="False"></asp:datagrid>
            </p>
            <p>
                <asp:DropDownList id="ddlColSelect" runat="server"></asp:DropDownList>
                <asp:Button id="btnShow" onclick="btnShow_Click" runat="server" Text="Show"></asp:Button>
                <asp:Button id="btnHide" onclick="btnHide_Click" runat="server" Text="Hide"></asp:Button>
            </p>
        </form>
    </body>
    </html>