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

发布于 2024-08-30 19:15:35 字数 349 浏览 8 评论 0原文

我将一个表绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦开始←不甜 2024-09-06 19:15:35

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

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

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);
    }
浅听莫相离 2024-09-06 19:15:35

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

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

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

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
    e.Row.Cells[0].Attributes["width"] = "200px";
}
我ぃ本無心為│何有愛 2024-09-06 19:15:35

我喜欢尽可能回答我自己的问题,以便将来搜索该线程的用户能够找到答案。

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

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

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;
}
奢华的一滴泪 2024-09-06 19:15:35

我这样做是:

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

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";
五里雾 2024-09-06 19:15:35

我会这样做:

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

I'd do it like this:

foreach (DataControlField field in grdIssues.Columns)
{
  field.HeaderStyle.Width = 100;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文