我是在代码中执行把数据绑定到控件Gridview上的,我执行查询的时候Gridview里面已经显示了8行数据
然后我执行下面的代码
int columns = Convert.ToInt32(DropDownList1.SelectedValue) - 1;
//得到当前的正确列数
GridView1.Columns[columns].Visible = false;
索引超出范围。必须为非负值并小于集合大小。
参数名: index
其实这个时候columns=7;
然后我执行下面的代码
int columns = Convert.ToInt32(DropDownList1.SelectedValue) - 1;
//得到当前的正确列数
GridView1.Columns[columns].Visible = false;
索引超出范围。必须为非负值并小于集合大小。
参数名: index
其实这个时候columns=7;
不然怎么设置
<asp:BoundField DataField="BH" HeaderText="">
<ControlStyle CssClass="hidden" />
<FooterStyle CssClass="hidden"/>
<HeaderStyle CssClass="hidden"/>
<ItemStyle CssClass="hidden"/>
</asp:BoundField>
.hidden
{
display:none;
}
设置visibl=false是取页面上是取不到那列的,就是7列啦,用上面的css代码隐藏就可以取到隐藏列
提示错误
错误 2 类型“System.Web.UI.WebControls.GridView”不具有名为“ItemStyle”的公共属性。 D:\excel\程序\Default.aspx 80
初步判断,你可能DropDownList的选择事件后,设置GridView1的隐藏,但是此时你的GridView1由于回传,还没有来得及绑定,所以你此时的GridView1的数据还是空的。
建议你在绑定我绑定数据的代码时来隐藏需要的列:
OleDbDataAdapter olesda = new OleDbDataAdapter(yuju, oledbcon);
DataSet ds = new DataSet();
olesda.Fill(ds, "dat");
GridView1.DataSource = ds.Tables["dat"];
GridView1.DataBind();
int columns = Convert.ToInt32(DropDownList1.SelectedValue) - 1;
GridView1.Columns[columns].Visible = false;
olesda.Dispose();
ds.Dispose();
oledbcon.Close();
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection cells1 = e.Row.Cells;
ViewState["no"] = cells1.Count.ToString();//这就是你要的列数
}
}在GridView的PreRender事件中设置
protected void GridView1_PreRender(object sender, EventArgs e)
{
if(GridView1.Rows.Count>0)
GridView1.Columns[Convert.ToInt16(ViewState["no"])-1].Visible = false;
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection cells1 = e.Row.Cells;
int i1 = cells1.Count;
cells1[i1 - 1].Visible = false;//隐藏最后一列标题
} if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCellCollection cells2 = e.Row.Cells;
int i2 = cells2.Count;
cells2[i2 - 1].Visible = false;//隐藏最后一列数据
}
}
我不如不查询这列就好了
我只是在查询的时候想看到
然后在要打印的时候把他给隐藏掉
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[7].Visible = false;
}
}
当然你还应该考虑隐藏标头,其中的7,应该是RowCreated事件下获得!!
贴子不是隐藏列吗??
隐藏表头:
GridView1.HeaderRow.Cells[7].Visible = false;
http://blog.csdn.net/hustypf/article/details/7797333
C#中GridView隐藏列的方法
下面有两种方法,不仅可以隐藏,还仍然可以正常取值
方法一:
在RowCreated事件中书写如下代码 void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
}
//可以根据需要设置更多的列
}
因为在RowCreated事件(隐藏)在绑定时候发生,所以这样就即能将数据绑定到列上,又隐藏了该列.所
以可以访问到隐藏列的值
方法二:
Public void myTestFunction()
{
string conString="....";//省略
string sqlquery="...";//省略
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
GridView1.DataSouce = ds.Tables[0];
GridView1.DataBind() ;}
{
if (e.Row.RowType != DataControlRowType.Pager)//如果不是
{
//将GRIDVIEW的第一列隐藏
e.Row.Cells[0].Visible = false;
}
}