Why can't I set the GridView column width?

M

mohaaron

OK, I have now gone through lots of posts, and there are lots, looking
for a anwer as to why I can't set the width of the columns in my
gridview. So far I have tried the following methods to set the width
and nothing works.

1. <asp:BoundField HeaderText="fieldName" ItemStyle-Width="300px"
DataField="fieldName" />
2. <asp:BoundField HeaderText="fieldName" HeaderStyle-Width="300px"
DataField="fieldName" />
3. This has been tried in both the RowCreated and RowDataBound events.
foreach (TableCell cell in e.Row.Cells)
{
int width = cell.Text.Length * 10;
cell.Width = Unit.Pixel(width);
}
4. This has been tried in both the RowCreated and RowDataBound events.
foreach (DataControlField field in this.searchGrid.Columns)
{
field.ItemStyle.Width = Unit.Pixel(500);
}

Why is it so diffecult to do something that should be so simple?
 
A

Alexey Smirnov

OK, I have now gone through lots of posts, and there are lots, looking
for a anwer as to why I can't set the width of the columns in my
gridview. So far I have tried the following methods to set the width
and nothing works.

1. <asp:BoundField HeaderText="fieldName" ItemStyle-Width="300px"
DataField="fieldName" />
2. <asp:BoundField HeaderText="fieldName" HeaderStyle-Width="300px"
DataField="fieldName" />
3. This has been tried in both the RowCreated and RowDataBound events.
foreach (TableCell cell in e.Row.Cells)
{
int width = cell.Text.Length * 10;
cell.Width = Unit.Pixel(width);}

4. This has been tried in both the RowCreated and RowDataBound events.
foreach (DataControlField field in this.searchGrid.Columns)
{
field.ItemStyle.Width = Unit.Pixel(500);

}

Why is it so diffecult to do something that should be so simple?

A gridview will generate a table with columns width equal to the
ItemStyle-Width, or HeaderStyle-Width. The values 1), 2) should work
but it depends on a number of things... For example, if gridview has a
fixed width, or header is off (HeaderStyle-Width), etc.
 
B

Barrie Wilson

OK, I have now gone through lots of posts, and there are lots, looking
for a anwer as to why I can't set the width of the columns in my
gridview. So far I have tried the following methods to set the width
and nothing works.

1. <asp:BoundField HeaderText="fieldName" ItemStyle-Width="300px"
DataField="fieldName" />

this works ... but if you set it to "1" it will ignore the setting ... I
guess I'm not clear why you are setting/re-setting column width every time a
row is bound to the grid in the examples below ... doesn't seem to make
sense ...
 
M

mohaaron

OK, to answer both of your posts.

Alexey,

The GridView is not fixed with, there is no width set for it at all. I
would assume that if I set the width of the individual columns the
grid as a whole would take up the sum width of the columns. So I still
don't know why any of the methods I have tried don't work.

Barrie,

The width is not set to 1 so it should work, right? There is no
particular reason why I'm setting/re-setting the width when the row is
bound other then I thought I could use the length of the data in the
cell as a baseline for with width of the column. This of course also
not not working like everything that I have tried.

What now?
 
S

shaun.laughton

Hi

I am experiencing the very same problem as yourself, and have spent
many hours attempting to set column widths without succes. I don't
suppose you have found or been pointed in the right direction to a
solution to this have you?

Cheers

Shaun
 
S

shaun.laughton

Not sure whether this is the same for you, but I was using the CSS
Friednly adapters on my site, and this was forcing all GridView
controls on m site to te default formatting. This was being applied
dispite the fact that I was overiding this with a HeaderStyle on each
control. Commenting out the control from the
CSSFriendlyAdapters.browser file under APP_Browsers fixed my problem.

As I say, if you are using the CSS Friendly Adapters this will
probably fix it for you too. If not - I have no idea :blush:)

Cheers

Shaun
 
C

connie truong

i had the same problem.
the only way to force it to work is to convert the column to a template field, then you can set the width property and it will work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top