Asp.net 当AutoGenerateColumns=”时,动态设置gridview列的宽度;“真的”;

Asp.net 当AutoGenerateColumns=”时,动态设置gridview列的宽度;“真的”;,asp.net,.net,html,css,vb.net,Asp.net,.net,Html,Css,Vb.net,当我将属性AutoGenerateColumns设置为AutoGenerateColumns=“true”时,在设置gridview的宽度时遇到问题。gridview是代码隐藏中的数据绑定。如果我使用的是gridview1.columns(0).width,则会引发错误 并且GridView1.Columns.Count始终为零,因为网格视图是数据绑定的 在.aspx中: <asp:GridView ID="GridView1" runat="server" AutoGenerateCol

当我将属性AutoGenerateColumns设置为AutoGenerateColumns=“true”时,在设置gridview的宽度时遇到问题。gridview是代码隐藏中的数据绑定。如果我使用的是gridview1.columns(0).width,则会引发错误

并且GridView1.Columns.Count始终为零,因为网格视图是数据绑定的

在.aspx中:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
因此,myTableName有更多的列,我不喜欢通过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部分

但你走的是对的。我,我用了一些像这样的东西,它一直在工作

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
但是请记住,gridview呈现为一个表。因此,单元格将自身调整为最长的内容。

我知道了

以下是.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>
实际上,当我们使用boundfields时,gridview列宽度会在浏览器中渲染,因为我们设置了每一列的宽度。我在两个项目中使用了两种方法——一种是通过使用AutoGenerateColumns=“false”获取绑定字段,另一种是通过设置AutoGenerateColumns=“true”——分别在两个项目中使用,然后当页面在浏览器中呈现时,我使用了浏览器的“查看源”功能,然后意识到这两种类型的主要区别是什么。差异如下:

style="table-layout:fixed;" 
我还在gridview标记的.aspx页面中添加了以下行:-

style="table-layout:fixed;" Width="1000px" 
现在它工作得很好


谢谢大家

如果您不打算使网格处于固定模式(即,由于有大量列,因此预期会出现溢出行为),则上述带有style=“table layout:fixed;”的解决方案并不合适

e、 g.参见以下场景:

<div style="overflow:auto;">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
</div>

提供完整的
RowDataBound
方法正文。你用
If
子句签入什么?@YuriyRozhovetskiy很抱歉,它是错误添加的。谢谢,我也一样。表的大小被调整,所有的工作都变得毫无价值@noisyas2对我来说,它在没有:Width=“1000px”的情况下工作得很好,但可能是这样,因为我使用了类似cell2.Width=New Unit(“10%”)的东西,并以百分比计算每个单元格的宽度。
style="table-layout:fixed;" Width="1000px" 
<div style="overflow:auto;">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
</div>
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