首页 > 解决方案 > 设置 Gridview 列宽

问题描述

我的 asp.net 页面中有两个网格 - vb.net,autogeneratecolumn 设置为 true。我绑定了列相同但数据不同的两个网格。现在我想在grid2中设置列的宽度,就像在grid1中一样。任何人都可以帮助我。我试图在后面的代码中设置为:

Private Sub grid2_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grid2.RowDataBound
    For i = 0 To grid1.Rows(0).Cells.Count - 1
        e.Row.Cells(i).Width = grid1.HeaderRow.Cells(i).Width
    Next
End Sub

grid1.HeaderRow.Cells(i).Width给我 0 作为价值。

标签: asp.netvb.netgridview

解决方案


尝试在 Gridview 的 RowCreated 事件上编写代码,如下所示:

Private Sub grid2_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles grid2.RowCreated
For i = 0 To grid1.Rows(0).Cells.Count - 1
    e.Row.Cells(i).Width = grid1.HeaderRow.Cells(i).Width
}  

推荐阅读