AutoGenerateColumns 为 true 时如何获取 gridview 列的宽度
我有一个 gridview,我需要在 gridview 进行数据绑定后获取列的宽度。我必须有 AutoGenerateColumns=true 因为 gridview 绑定到的数据表的列不会被修复。我遇到的问题是当 AutoGenerateColumns 为 true 时,不会填充 gridview 列集合。无论如何,我可以在 gridview 数据绑定后获取每列的宽度吗?我尝试使用 RowDataBound 并浏览表格单元格,但每列的宽度均为 0。
I have a gridview that I need to get the width of columns for after the gridview is databound. I have to have AutoGenerateColumns=true because the columns for the datatable that the gridview is bound to is not going to be fixed. The problem I'm having is the gridview columns collection is not populated when AutoGenerateColumns is true. Is there anyway I can get the width of each column after the gridview is databound? I've tried using RowDataBound and going through the tablecells but the width is 0 for every column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您将列的宽度设置为特定大小(您没有),否则至少在渲染 Page_Render 事件之前您不可能知道生成的列的宽度。在此之前,控件的 html 并不存在,并且在页面渲染为 html 并处理附加的 css 之前,无法知道其宽度。
Unless you are setting the width of the column to a specific size (you aren't) you can't possibly know the width of the generated column until at least the render Page_Render event. The control's html does not exist until then and its width can't be known until the page is rendered into html and the css attached processed.
也许在控件的生命周期中访问 GridView 的属性还为时过早。您是否尝试过在 DataBound 事件中访问 GridView 的列宽?有关详细信息,请参阅 ASP.NET 页面生命周期概述。
数据绑定事件:
Perhaps this is too early in the control's life cycle to access the GridView's properties. Have you tried accessing the GridView's column width in the DataBound Event? See the ASP.NET Page Life Cycle Overview for more information.
DataBound Event: