我进错了。不过我也有点想法,呵呵~~  ^_^datagrid绑定数据源后选择自动生成列,然后在dropdownlist添加selectIndexChange事件,然后在该事件里面对执行查询的语句进行改动就行拉

解决方案 »

  1.   

    这种问题都不知道碰到多少遍啦,都是一些小问题,自己多钻下
    http://singlepine.cnblogs.com/articles/266538.html
    这里有个很齐全的datagrid实例,你自己多琢磨琢磨,对以后你碰到的问题也会有帮助!
    补充一点,也是初学者容易忘记的
    dropdownlist的autopostback属性记得改为true
      

  2.   

    下面说我想的方案二:
    仍然是dropdownlist添加selectIndexChange事件,对datagrid的相应的列进行使其隐藏和显示的操作,这个比较简单  ^_^
      

  3.   

    呵呵,也是孟子大哥的fans,那就等孟子亲自解决这个问题吧
      

  4.   

    呵呵,也是孟子大哥的fans,那就等孟子亲自解决这个问题吧
    ===========================================================杀鸡何用牛刀呀  ^_^
      

  5.   

    DataGrid控件的列有一个属性,可以设置这个列为显示或隐藏,接下来楼主应该知道怎么做了。其实这样的问题很简单,自已多想想、多查查MSDN问题就解决了。
      

  6.   

    以编程方式隐藏数据网格中的列
    在窗体的声明区域中声明 DataGridTableStyle 类的新实例。将 DataGridTableStyle 的 MappingName 属性设置为希望应用样式的数据源中的表。下面的代码示例使用 DataGrid.DataMember 属性(假定已经设置了它)。向数据网格的表样式集合中添加新的 DataGridTableStyle 对象。通过将列的 Width 属性设置为 0,并指定要隐藏的列的列索引来隐藏列。 Visual Basic  复制代码 
    ' Declare a new DataGridTableStyle in the
    ' declarations area of your form.
    Dim ts As DataGridTableStyle = New DataGridTableStyle()Sub HideColumn()
       ' Set the DataGridTableStyle.MappingName property
       ' to the table in the data source to map to.
       ts.MappingName = DataGrid1.DataMember   ' Add it to the datagrid's TableStyles collection
       DataGrid1.TableStyles.Add(ts)   ' Hide the first column (index 0)
       DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0
    End Sub 
    C#  复制代码 
    // Declare a new DataGridTableStyle in the
    // declarations area of your form.
    DataGridTableStyle ts = new DataGridTableStyle();private void hideColumn()
    {
       // Set the DataGridTableStyle.MappingName property
       // to the table in the data source to map to.
       ts.MappingName = dataGrid1.DataMember;   // Add it to the datagrid's TableStyles collection
       dataGrid1.TableStyles.Add(ts);   // Hide the first column (index 0)
       dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 0;
    }
      

  7.   

    if (显示)
             {
                DataGrid.Columns[0].Visible = true;
             }
             else
             {
                DataGrid.Columns[0].Visible = false;
             }不好意思,在WebForm下面用这个方法!
    我上面的方法是Winform里的!
      

  8.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴)

    amandag(高歌) 
    的答案都可以,当要显示 "性别","所在省","入职时间"等字段的 任一字段改成显示任N个字段时,又该怎么办呢?也就是说有N个dropdownlist.请大哥帮忙解决一下,这个问题总共300分,当高分赠送,谢谢
      

  9.   

    个人建议你用CheckBoxList,这样容易解决一些
    //aspx
    <form id="Form1" method="post" runat="server">
    <asp:CheckBoxList id="CheckBoxList1" runat="server"></asp:CheckBoxList>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="au_id" HeaderText="au_id"></asp:BoundColumn>
    <asp:BoundColumn DataField="au_lname" HeaderText="au_lname"></asp:BoundColumn>
    <asp:BoundColumn DataField="au_fname" HeaderText="au_fname"></asp:BoundColumn>
    <asp:BoundColumn DataField="phone" HeaderText="phoneX"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    </form>//aspx.cs
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    string [] str = {"au_lname","au_fname", "phone"};
    CheckBoxList1.DataSource = str;
    CheckBoxList1.DataBind();
    }
    }private void Button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs");
    SqlCommand cmd = new SqlCommand("select au_id, au_lname, au_fname, phone from authors", cn);
    cn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DataGrid1.DataSource = dr;
    for(int i = 1; i < DataGrid1.Columns.Count; i++)
    {
    DataGrid1.Columns[i].Visible = false;
    }
    for(int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
    if(CheckBoxList1.Items[i].Selected)
    DataGrid1.Columns[i + 1].Visible = true;
    } DataGrid1.DataBind();
    dr.Close();
    cn.Close();
    }
      

  10.   

    不用多个downlist
    你在downlist数据前加个复选框,绑定你的id字段.用数组接收你选中的的ID
    在selectIndexChange的事件里做你想做的...
      

  11.   

    dropdownlist是在datagrid的页眉中的
      

  12.   

    dropdownlist添加selectIndexChange事件就可以了,在cs代码中再写上对应的操作
    // <summary>
        /// 在datalist中添加dropdownlist事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DropTelLogFlag(object sender, System.EventArgs e)
        {
            //建立droptellogflag下拉列表控件的引用对象   
            DropDownList droptellogflag = (DropDownList)sender;
            //取得选择内容
    ...............加载你的内容
        }
      

  13.   

    点击的dropdownlist所在的列位置次序不变动
      

  14.   

    个人觉得,用CheckBoxList比较实际些,你这要多少个dropdownlist?而且里面的内容都是什么?
      

  15.   

    里面内容都一样,都是表中的字段名,选什么字段名就显示什么字段,任意显示两列或两列以上,只是为了让浏览者可同时看多个自选的字段,而不用像任选一个那样每次只能看到一个自选的,当要看另一个自选的时,又要再点击dropdownlist
      

  16.   

    事件触发以后
    就可以控制数据的显示啊比如重新查询 然后重新DataBind();
      

  17.   

    dropdownlist添加selectIndexChange事件就可以了,在cs代码中再写上对应的操作
    // <summary>
        /// 在datalist中添加dropdownlist事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DropTelLogFlag(object sender, System.EventArgs e)
        {
            //建立droptellogflag下拉列表控件的引用对象   
            DropDownList droptellogflag = (DropDownList)sender;
            //取得选择内容
    ...............加载你的内容
        }
      

  18.   

    不要用多个DropdownList,你可以用一个CheckBoxList或者其它可以多选的控件。
      

  19.   

    还是希望在datagrid页眉上用多个dropdownlist解决,这样外观比较好,.
    当点击dropdownlist改时,代替的字段也能在点击的这一列显示,而不跑到其他位置次序
      

  20.   

    ddvv8899() ( ) 信誉:100    Blog   加为好友  2007-5-5 12:19:38  得分: 0  
        
    还是希望在datagrid页眉上用多个dropdownlist解决,这样外观比较好,.
    当点击dropdownlist改时,代替的字段也能在点击的这一列显示,而不跑到其他位置次序-----------------------------------------------------------------------------  
    这句话什么意思??
    我怎么看不懂?
      

  21.   

    比如:下面的datagrid
    ----------------------------------
    | id  |  name  |  sex |  didian  |
    ----------------------------------  
    | 1   |  张三  |  男  |   广东   |
    ----------------------------------
    | 2   |  李四  |  男  |   北京   |
    ----------------------------------
    | 3   |  王五  |  女  |   山东   | 
    ----------------------------------
    其中,sex和didian两列的页眉为dropdownlist,dropdownlist中包含一些字段名,如:sex,didian,
    rztime,age(年龄),job(职务)等;当点击sex列页眉的dropdownlist,选择job字段时,如果隐藏的job字段是在didian字段之后,那么job字段显示时,就会移到didian列后面,能不能显示时,让它的位置还是在原来的sex列位置处.    
      

  22.   

    to  wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) 具体代码该怎样写呢?
      

  23.   

    DataGridColumn column=DataGrid.Columns[indexold];
    DataGrid.Columns.Remove(column);
    DataGrid.Columns.AddAt(indexnew,column);indexold就是这个列当前的序号。如果你已经取到这个列的话,第一行可以省略。
    indexnew就是这个列新的位置。