二:托动一个GridView到页面,设置数据源.
三.托动一个DropList
如图
其中DropDownList1代码: 记住DropDownList1控件一定要设置AutoPostBack为true,不然就不能显示正常的结果,我当时也没有这么做,害我找了好久错在哪儿,笨啊!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1; i <= GridView1.Columns .Count ; i++)
DropDownList1.Items.Add(i.ToString());//填充数据为表列数
}
}
双击隐藏列和显示列按扭进处代码编辑:
protected void Button1_Click(object sender, EventArgs e)
{
int columns = Convert.ToInt32(DropDownList1 .SelectedValue)-1;//选中的列数字
GridView1 .Columns [columns ].Visible =false ; //使列隐藏
}
protected void Button2_Click(object sender, EventArgs e)
{
int cn = Convert.ToInt32(DropDownList1 .SelectedValue)-1;
if (GridView1.Columns[cn].Visible == false)//假如没有显示该列则显示
GridView1.Columns[cn].Visible = true;
}
本文介绍了如何在ASP.NET中使用GridView控件动态隐藏和显示列。通过拖放一个GridView到页面并设置数据源,然后添加一个DropDownList来选择列,并确保DropDownList的AutoPostBack属性设为true。在Button1_Click事件中,根据DropDownList选择的列号隐藏相应列;在Button2_Click事件中,如果列未显示,则将其重新显示。
680

被折叠的 条评论
为什么被折叠?



