当AutoGenerateColumns="true"时,动态设置网格视图列的宽度

3

当我使用AutoGenerateColumns属性时,设定gridview的宽度出现了问题,因为它是在代码后端绑定的。如果我使用gridview1.columns(0).width,则会引发错误。

而且,由于网格视图是数据绑定的,GridView1.Columns.Count始终为零。

在.aspx中:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>

在代码后台
Dim strCon As New SqlConnection("Data Source=myDataSource;Initial Catalog=myDataBaseName;Persist Security Info=True;User ID=GKRANJAN;Password=abcdef")
        Dim da As New SqlDataAdapter("Select * from myTableName", strCon)
        Dim ds As New DataSet
        da.Fill(ds)
        GridView1.DataSource = ds
        GridView1.DataBind()

因此,我的表名有更多的列,我不喜欢通过BoundField添加它们,因为在我的情况下它们是变化的。
在GridView1_RowDataBound中,我使用了:-
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim cell As TableCell = e.Row.Cells(0)
            cell.Width = New Unit("200px")
    End Sub

但是它对我没有用。请帮助我!谢谢大家!

提供完整的“RowDataBound”方法主体。你在其中使用“If”语句检查什么? - Yuriy Rozhovetskiy
@YuriyRozhovetskiy 对不起,这是错误添加的。谢谢。 - Gopal Krishna Ranjan
3个回答

3

我明白了。

以下是 .aspx 页面: -

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 

        style="table-layout:fixed;" Width="1000px">        

        <!-- Mind the above two lines to make this trick effective you must have to use both properties as is; -->

        </asp:GridView>
    </div>
    </form>
</body>

以下是代码后端:

Imports System.Data.SqlClient
Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strCon As New SqlConnection("Data Source=myDataSource;Initial Catalog=myDataBaseName;Persist Security Info=True;User ID=GKRANJAN;Password=abcdef")
        Dim da As New SqlDataAdapter("Select * from myTableName", strCon)
        Dim ds As New DataSet
        da.Fill(ds)
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Sub

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then

            'For first column set to 200 px
            Dim cell As TableCell = e.Row.Cells(0)
            cell.Width = New Unit("200px")

            'For others set to 50 px
            'You can set all the width individually

            For i = 1 To e.Row.Cells.Count - 1
                'Mind that i used i=1 not 0 because the width of cells(0) has already been set
                Dim cell2 As TableCell = e.Row.Cells(i)
                cell2.Width = New Unit("10px")
            Next
        End If
    End Sub
End Class 

实际上,当我们使用绑定字段时,网格视图列的宽度会在浏览器中呈现,因为我们设置了每个列的宽度。我在两个项目中使用了两种方法 - 一种是使用AutoGenerateColumns="false"的绑定字段,另一种是将AutoGenerateColumns = "true"设置为“true” - 在两个项目中分别使用,然后当页面在浏览器中呈现时,我使用浏览器的“查看源代码”功能,然后意识到两种类型之间的主要区别是:-

style="table-layout:fixed;" 

我在我的.aspx页面的gridview标签下添加了以下行: -
style="table-layout:fixed;" Width="1000px" 

现在它运行良好。

感谢所有人!!


对我来说,没有使用“Width =”1000px"也能正常工作。但可能是因为我使用了类似于cell2.Width = New Unit("10%")的东西,并且按百分比计算每个单元格的宽度。 - Wilhelm

2
我不知道是否只是一个打字错误(或者你省略了它),但是你在RowDataBound部分的代码缺少IF部分.. 但是你走在了正确的轨道上。我使用类似这样的东西,它一直都有效。
Protected Sub gvData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvData.RowDataBound
     If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(0).Width = New Unit("200px")
            e.Row.Cells(1).Width = New Unit("500px")
     End If
 End Sub

但是请记住,网格视图被呈现为一个表格。因此,单元格将根据最长内容自动调整大小。


和我发生了同样的事情。表格被重新调整大小,所有完成的工作都变得毫无价值。 @noisyass2 - Gopal Krishna Ranjan

2
如果您不打算将网格设置为固定模式(即期望溢出行为,因为列数很大),那么使用style="table-layout:fixed;"的上述解决方案并不合适。
例如,请参见以下情况:
<div style="overflow:auto;">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
</div>

在这种情况下,只需将单元格宽度设置为特定值,并将单元格换行设置为False。
Protected Sub gvData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvData.RowDataBound
     If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(0).Width = New Unit("200px") 
        e.Row.Cells(0).Wrap = false
     End If
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接