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

时间:2022-06-21 22:44:19

I am binding a table to a gridview in asp.net as such

我正在将表绑定到asp.net中的gridview

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

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

 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.

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

5 个解决方案

#1


6  

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);
    }

#2


3  

I was able to change the width of a certain Gridview column (bound to a Datatable) with the RowDataBound event:

我能够使用RowDataBound事件更改某个Gridview列的宽度(绑定到Datatable):

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
    e.Row.Cells[0].Attributes["width"] = "200px";
}

#3


0  

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.

我找不到直接做我想要的方法。但是我发现如果我自己定义列,我可以更改属性。在这个例子中,我想使列数据居中。像这样的东西。

BoundField bdfRaisedDate = new BoundField();
clsUtilities.SetBoundFieldCenter(ref bdfRaisedDate, "RaisedDateShort",    "Opened", "RaisedDate");

grdIssues.Columns.Add(bdfRaisedDate);

grdIssues.DataSource = mdtIssues;

grdIssues.DataBind();

public static void SetBoundFieldCenter(ref BoundField bdfAny, string pDataField, string pHeadingValue, string  pSortExpression)
{
      bdfAny.DataField = pDataField;
      bdfAny.HeaderText = pHeadingValue;
      bdfAny.SortExpression = pSortExpression;
      bdfAny.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
      bdfAny.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
}

#4


0  

I did it as:

我做到了:

gridView1.HeaderRow.Cells[0].Attributes["Width"] = "100px";
gridView1.HeaderRow.Cells[1].Attributes["Width"] = "50px";
gridView1.HeaderRow.Cells[2].Attributes["Width"] = "200px";

#5


-1  

I'd do it like this:

我这样做:

foreach (DataControlField field in grdIssues.Columns)
{
  field.HeaderStyle.Width = 100;
}

#1


6  

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);
    }

#2


3  

I was able to change the width of a certain Gridview column (bound to a Datatable) with the RowDataBound event:

我能够使用RowDataBound事件更改某个Gridview列的宽度(绑定到Datatable):

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
    e.Row.Cells[0].Attributes["width"] = "200px";
}

#3


0  

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.

我找不到直接做我想要的方法。但是我发现如果我自己定义列,我可以更改属性。在这个例子中,我想使列数据居中。像这样的东西。

BoundField bdfRaisedDate = new BoundField();
clsUtilities.SetBoundFieldCenter(ref bdfRaisedDate, "RaisedDateShort",    "Opened", "RaisedDate");

grdIssues.Columns.Add(bdfRaisedDate);

grdIssues.DataSource = mdtIssues;

grdIssues.DataBind();

public static void SetBoundFieldCenter(ref BoundField bdfAny, string pDataField, string pHeadingValue, string  pSortExpression)
{
      bdfAny.DataField = pDataField;
      bdfAny.HeaderText = pHeadingValue;
      bdfAny.SortExpression = pSortExpression;
      bdfAny.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
      bdfAny.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
}

#4


0  

I did it as:

我做到了:

gridView1.HeaderRow.Cells[0].Attributes["Width"] = "100px";
gridView1.HeaderRow.Cells[1].Attributes["Width"] = "50px";
gridView1.HeaderRow.Cells[2].Attributes["Width"] = "200px";

#5


-1  

I'd do it like this:

我这样做:

foreach (DataControlField field in grdIssues.Columns)
{
  field.HeaderStyle.Width = 100;
}