Html 当 AutoGenerateColumns="true" 时动态设置 gridview 列的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8531383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:03:29  来源:igfitidea点击:

Set Width of gridview columns dynamically when AutoGenerateColumns="true"

asp.net.nethtmlcssvb.net

提问by Gopal Krishna Ranjan

I have a problem in setting the width of the gridview when i used the property AutoGenerateColumns to AutoGenerateColumns="true". And the gridview is databind in code behind. If i am using gridview1.columns(0).width it raise error.

当我使用属性 AutoGenerateColumns 到 AutoGenerateColumns="true" 时,我在设置 gridview 的宽度时遇到问题。网格视图在后面的代码中是数据绑定的。如果我使用 gridview1.columns(0).width 它会引发错误。

And the GridView1.Columns.Count is always zero because the grid view is databind.

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

In .aspx: -

在 .aspx 中:-

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

In code behind

在后面的代码中

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()

Hence myTableName has more columns and i dont like to add them through BoundFiled because they vary in my case.

因此 myTableName 有更多的列,我不喜欢通过 BoundFiled 添加它们,因为它们在我的情况下有所不同。

In GridView1_RowDataBound i used : -

在 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

But it could not work for me. Please help me!!

但它对我不起作用。请帮我!!

Thanks to all!!

谢谢大家!!

回答by Gopal Krishna Ranjan

I got it.

我知道了。

Below is the .aspx page: -

以下是 .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>

And this is the Code behind: -

这是背后的代码:-

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 

Actually when we use boundfields then gridview columns width renders in browser as we set the widths of each and every columns. I used two methods in two projects - that is one by taking bound fields with AutoGenerateColumns="false" and another by setting the AutoGenerateColumns = "true" - individually in two project and then when page got rendered in browser, i used "View Source" functionality of browser and then realized that what is the main difference in both types. The difference is as: -

实际上,当我们使用 boundfields 时,当我们设置每一列的宽度时,gridview 列宽度会在浏览器中呈现。我在两个项目中使用了两种方法 - 一种是通过使用 AutoGenerateColumns="false" 获取绑定字段,另一种是通过设置 AutoGenerateColumns = "true" - 在两个项目中单独使用,然后当页面在浏览器中呈现时,我使用了“查看源代码” " 浏览器的功能,然后意识到这两种类型的主要区别是什么。区别在于: -

style="table-layout:fixed;" 

I also added the below lines in my .aspx page at gridview tag: -

我还在我的 .aspx 页面中的 gridview 标签中添加了以下几行: -

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

And now it's working fine.

现在它工作正常。

Thanks to All!!

谢谢大家!!

回答by Lin

If you are not intending to make the grid in fixed mode (ie. expecting a overflow behaviour as there are large number of columns) then the above solution with style="table-layout:fixed;" is not the appropriate one.

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

e.g. see the below scenario:

例如,请参阅以下场景:

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

In this such case just set the cell Width to specific value and Set Cell Wrap to False

在这种情况下,只需将单元格宽度设置为特定值并将单元格环绕设置为 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

回答by noisyass2

I dont know if its just a typo(or you omitted it) but your code on the RowDataBound part is missing the IF part..

我不知道它是否只是一个错字(或者你省略了它)但是你在 RowDataBound 部分的代码缺少 IF 部分..

But youre on the right track. Me, i use some thing like this and it works all the time

但你在正确的轨道上。我,我使用这样的东西,它一直有效

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

But remember, the gridview is rendered a table. So cells will resize itself to the longest content.

但请记住,gridview 呈现为一个表格。因此,单元格将自身调整为最长的内容。