后面的代码中没有列。 [英] Columns not available in code behind.

查看:75
本文介绍了后面的代码中没有列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VB.NET和autogeneratecolumns = true绑定代码中的Gridview控件,而没有在源代码(html)代码或.aspx文件的模板中创建任何列。

当我运行它时,我可以在网页上看到两个列,但是当我希望在代码后面调整它们的宽度时,它不会识别任何列,可能是因为它是在嵌套上完成的或者保护级别。

如何设置这些嵌套列的可见性和宽度?

谢谢



我尝试了什么:



我尝试了十倍的事情,比如事先创建列,设置autogeneratecolumns = false并阅读关于Gridview。

I am binding a Gridview control in code behind with VB.NET and with autogeneratecolumns=true, without having created any columns in the source(html) code or templates of the .aspx file.
When I run it, I can see both columns on the web page, but when I wish to size their width in code behind, it does not recognise any columns, probably because it is done on a nested or protected level.
How can I set the visibility and width of these nested columns?
Thanks

What I have tried:

I have tried tenfold things, like creating columns beforehand, setting autogeneratecolumns=false and read your excellent article on the Gridview.

推荐答案

您不能使用以下代码在我们的代码中直接隐藏 AutoGenerateColumns



GridView1.Columns [index] .Visible = false;



为什么?



这是因为自动生成的列未添加到 GridView 集合。使用上面的代码将为您提供索引超出范围错误。



作为一种解决方法,这里有几种方法可以隐藏 GridView 中的特定列 AutoGenerateColumns 设置为 TRUE



选项1:使用 Cells index

You cannot "directly" hide AutoGenerateColumns in our codes using the code below:

GridView1.Columns[index].Visible = false;

Why?

This is because auto generated columns are not added in the GridView Columns collection. Using the code above will give you "index was out of range error".

As a workaround, here are a couple of ways on how to hide specific column in GridView with AutoGenerateColumns set to TRUE:

Option 1: Using the Cells index
<code></code>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
        //Just changed the index of cells based on your requirements
        e.Row.Cells[0].Visible = false;

}





选项2:循环播放 GridView 控制集合





Option 2: Looping through GridView Row Controls collections

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
        //Just changed the index of cells based on your requirements
        foreach (TableRow row in GridView1.Controls[0].Controls)
        {
            row.Cells[0].Visible = false;
        }

}





您可以使用上面的选项隐藏如果您确定 GridView 中的的顺序。请注意,自动生成的列将显示 DataSource 中的所有列,因此在使用index隐藏



选项3:循环播放 GridView Cells



您可能知道 GridView 单元格由不同的 DataControlFields <组成/ code>并且基本上 AutoGenerated 字段使用 BoundField 来显示数据。在这种情况下,我们可以遍历 GridView 生成的单元格,并将单元格转换为 DataControlFieldCell 键入以获取 ContainingField 然后我们可以将此 ContainingField 转换为 BoundField 以便我们检查特定 AutoGenerated 中使用的 DataField BoundField 并使用其可见属性隐藏它们。



为了使它更清晰,你可以查看下面的代码块:





You can use the options above for hiding the Columns if you are sure with the order of the Columns in your GridView. Please note that autogenerated columns will display all the columns from the DataSource, so you must be careful when using index for hiding the Columns.

Option 3: Looping through GridView Cells

As you may know the GridView cells are composed of different DataControlFields and basically AutoGenerated fields uses BoundFields for displaying the data. In this case we can loop through the cells generated by the GridView and cast the cell to a DataControlFieldCell type to get the ContainingField then we can cast this ContainingField to a BoundField so that we can check the DataField used in a particular AutoGenerated BoundField and hide them using its Visible property.

To make it more clearer then you can check this code block below:

Protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
   //Just set the Column Name that you wish to hide based on your requirements
        foreach (TableCell cell in e.Row.Cells)
        {
            BoundField field = (BoundField)((DataControlFieldCell)cell).ContainingField;
            if (field.DataField == "ColumnName")
            {
                field.Visible = false;
            }
        }

}





如你所见,我们检查了 ColumnName 首先隐藏列而不是使用索引



上面的 ColumnName 表示要隐藏的 DataSource 中的字段。如果您不知道 DataSource 中列的顺序,可以使用此选项隐藏列。



就是这样!希望你会发现这个例子很有用!



As you can see, we checked for the ColumnName first before hiding the column instead of using the index of Column.

The ColumnName above indicates the field from your DataSource that you want to hide. You can use this option for hiding the columns if you do not know the sequence of the columns from the DataSource.

That’s it! Hope you will find this example useful!


这篇关于后面的代码中没有列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆