以编程方式隐藏数据网格中的列 在窗体的声明区域中声明 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; }
http://singlepine.cnblogs.com/articles/266538.html
这里有个很齐全的datagrid实例,你自己多琢磨琢磨,对以后你碰到的问题也会有帮助!
补充一点,也是初学者容易忘记的
dropdownlist的autopostback属性记得改为true
仍然是dropdownlist添加selectIndexChange事件,对datagrid的相应的列进行使其隐藏和显示的操作,这个比较简单 ^_^
===========================================================杀鸡何用牛刀呀 ^_^
在窗体的声明区域中声明 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;
}
{
DataGrid.Columns[0].Visible = true;
}
else
{
DataGrid.Columns[0].Visible = false;
}不好意思,在WebForm下面用这个方法!
我上面的方法是Winform里的!
和
amandag(高歌)
的答案都可以,当要显示 "性别","所在省","入职时间"等字段的 任一字段改成显示任N个字段时,又该怎么办呢?也就是说有N个dropdownlist.请大哥帮忙解决一下,这个问题总共300分,当高分赠送,谢谢
//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();
}
你在downlist数据前加个复选框,绑定你的id字段.用数组接收你选中的的ID
在selectIndexChange的事件里做你想做的...
// <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;
//取得选择内容
...............加载你的内容
}
就可以控制数据的显示啊比如重新查询 然后重新DataBind();
// <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;
//取得选择内容
...............加载你的内容
}
当点击dropdownlist改时,代替的字段也能在点击的这一列显示,而不跑到其他位置次序
还是希望在datagrid页眉上用多个dropdownlist解决,这样外观比较好,.
当点击dropdownlist改时,代替的字段也能在点击的这一列显示,而不跑到其他位置次序-----------------------------------------------------------------------------
这句话什么意思??
我怎么看不懂?
----------------------------------
| id | name | sex | didian |
----------------------------------
| 1 | 张三 | 男 | 广东 |
----------------------------------
| 2 | 李四 | 男 | 北京 |
----------------------------------
| 3 | 王五 | 女 | 山东 |
----------------------------------
其中,sex和didian两列的页眉为dropdownlist,dropdownlist中包含一些字段名,如:sex,didian,
rztime,age(年龄),job(职务)等;当点击sex列页眉的dropdownlist,选择job字段时,如果隐藏的job字段是在didian字段之后,那么job字段显示时,就会移到didian列后面,能不能显示时,让它的位置还是在原来的sex列位置处.
DataGrid.Columns.Remove(column);
DataGrid.Columns.AddAt(indexnew,column);indexold就是这个列当前的序号。如果你已经取到这个列的话,第一行可以省略。
indexnew就是这个列新的位置。