绑定到数据表时如何设置 gridview 列宽
我将一个表绑定到 asp.net 中的 gridview ,
grdIssues.DataSource = mdtIssues;
grdIssues.DataBind();
问题是我无法控制列宽,asp.net 似乎自己决定每列的宽度应该是多少。诸如此类的方法
grdIssues.Columns[0].ItemStyle.Width = 100;
grdIssues.Columns[1].ItemStyle.Width = 100;
不起作用,因为列是动态创建的。我不敢相信除了手动创建每一列并填充每一行之外,没有其他方法可以做到这一点。
I am binding a table to a gridview in asp.net as such
grdIssues.DataSource = mdtIssues;
grdIssues.DataBind();
The problem is I cannot then control the column width, asp.net seems to decided on it's own what width each column should be. Methods such as
grdIssues.Columns[0].ItemStyle.Width = 100;
grdIssues.Columns[1].ItemStyle.Width = 100;
don't work because the columns are created dynamically. I cannot believe there isn't a way to do this short of manually creating each column and filling each row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不必手动创建列来设置它们的宽度,您可以这样做
You dont have to manually create the columns to set them the width, you can do this
我能够使用
RowDataBound
事件更改特定Gridview
列(绑定到Datatable
)的宽度:I was able to change the width of a certain
Gridview
column (bound to aDatatable
) with theRowDataBound
event:我喜欢尽可能回答我自己的问题,以便将来搜索该线程的用户能够找到答案。
我找不到直接做我想做的事情的方法。但是我发现如果我自己定义列,我可以更改属性。在此示例中,我想要将列数据居中。像这样的东西。
I like to answer my own question whenever I can so future users searching the thread will find the answer.
I could not find a way to do what I wanted directly. However I found if I define the columns myself, I could change the properties. In this example, I wanted to center the column data. Something like this.
我这样做是:
I did it as:
我会这样做:
I'd do it like this: