VB.Net-数据绑定后是否可以调整未绑定的GridView列的大小?

时间:2019-05-09 07:04:58

标签: asp.net vb.net visual-web-developer-2010

我当前的Web程序将SQL数据显示到网格视图中。

现在,我想在数据绑定完成后重新调整列的大小。因为某些列最终由于文本量而占用太多空间,所以我想调整列的大小以使显示看起来更整洁。

 Protected Sub loadInfo()

    Dim cmd As New SqlCommand
    Dim dsgroup As New DataSet

    Dim string1 As String = [SQL query here]
    Dim da As New SqlDataAdapter(string1, conn)

    cmd.Connection = conn

    conn.Open()
    da.Fill(dsgroup, "Display")

    Dim dvgroup As DataView = dsgroup.Tables("Display").DefaultView

    GridView1.DataSource = dvgroup
    GridView1.DataBind()

    GridView1.Columns(0).ItemStyle.Width = 50
    GridView1.Columns(1).ItemStyle.Width = 50
    GridView1.Columns(2).ItemStyle.Width = 50
    GridView1.Columns(3).ItemStyle.Width = 50
    GridView1.Columns(4).ItemStyle.Width = 50
    GridView1.Columns(5).ItemStyle.Width = 50
    GridView1.Columns(6).ItemStyle.Width = 50
    GridView1.Columns(7).ItemStyle.Width = 50
    GridView1.Columns(8).ItemStyle.Width = 50
    GridView1.Columns(9).ItemStyle.Width = 50

    conn.Close()

End Sub

为了测试,我首先尝试将每个列的大小设置为50。但是在测试后,我在行GridView1.Columns(0).ItemStyle.Width = 50 上遇到了此错误:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index 

要使此功能生效,我还应该添加什么?

0 个答案:

没有答案