2010-03-23 79 views
1

如何使一个列在GridView中不可见?我试图用这个:如何在GridView中使列不可见?

dataGridView.Columns(0).Visible = False 

但它得到一个错误"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"。我怎样才能做到这一点?

+0

AutoGenerateColumns是否设置为true?我认为自动生成的列不包含在Columns集合中。 – rodrigocl 2012-01-05 13:54:16

回答

2

最后,我得到了答案。使用这个我们可以使一列不可见:

Private Sub dataGridView_RowDataBound(_ 
    ByVal sender As Object, _ 
    ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs _ 
) Handles dataGridView.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow OrElse _ 
     e.Row.RowType = DataControlRowType.Header OrElse _ 
     e.Row.RowType = DataControlRowType.Footer _ 
    Then 
     e.Row.Cells(1).Visible = False 
    End If 
End Sub 
+1

哎哟,这是一种痛苦的方式...尝试获取页面的PreRender事件中的列,然后设置其可见性。 – slugster 2010-03-23 05:34:01

+0

你需要'if'语句吗? – Matt 2010-03-23 10:45:32

1

我不明白为什么不与你合作。

它的工作只是找到我 - 至少在c#上。

检查看看哪里出了问题,也许你在DataGrid呈现/创建之前或之前调用它。

1

您的语句“dataGridView.Columns(0).Visible = False”没问题,您只需修复索引即可。列和细胞指数为基础1,不垒0

如果你想看不见的第一列使用:

dataGridView.Columns(1)。可见=假

这对我的作品。

相关问题