如何在gridview中使列不可见?

时间:2010-03-23 05:02:22

标签: asp.net vb.net

如何在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"。我怎么能这样做?

3 个答案:

答案 0 :(得分: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 :(得分:1)

我无法理解为什么不和你合作。

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

检查出现了什么问题,也许你在渲染/创建DataGrid之前调用它或类似的东西。

答案 2 :(得分:1)

您的语句“dataGridView.Columns(0).Visible = False”没问题,您只需修复索引即可。 Columns和Cell索引是基数1,而不是基数0。

如果要使第一列不可见,请使用:

dataGridView.Columns(1).Visible = False

这适合我。

相关问题