vb.net Gridview 显示和隐藏特定列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8755773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gridview Show and Hide a Specific Column
提问by Adeel Aslam
I have a gridview in which a specific column Date
. I have set the Visible
property of column to false because I want to show on different conditions of page. Please tell me how can I do it using vb.net that my Date
column should show or hide at runtime
我有一个 gridview,其中有一个特定的列Date
。我已将Visible
列的属性设置为 false,因为我想在页面的不同条件下显示。请告诉我如何使用 vb.net 来实现我的Date
专栏应该在运行时显示或隐藏
Update
更新
My current code is
我目前的代码是
If Not Page.User.Identity.Name = "bilal" Then
GridView1.AutoGenerateEditButton = False
GridView2.AutoGenerateEditButton = False
GridView3.AutoGenerateEditButton = False
Else
GridView1.AutoGenerateEditButton = True
GridView1.AutoGenerateColumns = True
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the first '
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(0).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(0).Visible = True
Next
End If
GridView2.AutoGenerateEditButton = True
GridView3.AutoGenerateEditButton = True
End If
回答by Tim Schmelter
If you've set AutoGenerateColumns
to True
, the Column-Count will be 0, then you need to loop the rows and show/hide the appropriate cells. Otherwise you can use the Visible
property.
如果您已设置AutoGenerateColumns
为True
,则 Column-Count 将为 0,然后您需要循环行并显示/隐藏适当的单元格。否则,您可以使用该Visible
属性。
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the 4.'
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(3).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(3).Visible = True
Next
End If