如何在绑定到数据表时设置gridview列的宽度

如何在绑定到数据表时设置gridview列的宽度

本文介绍了如何在绑定到数据表时设置gridview列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net中将一个表绑定到一个gridview中。

  grdIssues.DataSource = mdtIssues; 

grdIssues.DataBind();

问题是我无法控制列的宽度,asp.net似乎决定使用它自己的每列的宽度应该是。方法如

  grdIssues.Columns [0] .ItemStyle.Width = 100; 
grdIssues.Columns [1] .ItemStyle.Width = 100;

不起作用,因为列是动态创建的。我不敢相信没有办法做到这一点,但没有手动创建每一列并填充每一行。

你不用必须手动创建列来设置它们的宽度,您可以这样做

  foreach(OrdersGV.Columns中的DataControlField列)
{
column.ItemStyle.Width = Unit.Pixel(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.

解决方案

You dont have to manually create the columns to set them the width, you can do this

 foreach (DataControlField column in OrdersGV.Columns)
    {
      column.ItemStyle.Width = Unit.Pixel(100);
    }

这篇关于如何在绑定到数据表时设置gridview列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:34