How to Set a gridview column width when binding to a datatable

Bob Avallone picture Bob Avallone · Apr 29, 2010 · Viewed 56.8k times · Source

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.

Answer

alejandrobog picture alejandrobog · Apr 29, 2010

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