21
votes

I can not set the width of bound field. Is there any problem in the following markup.

 <asp:BoundField DataField="UserName" HeaderText="User Name"
                   meta:resourcekey="BoundFieldUNCUserNameResource1">
        <HeaderStyle Width="50%" />
 </asp:BoundField>

enter image description here

Please refer to the image. I set width using the following. The yellow colored numbers are corresponding width. The marked user name is always Wrapped even I set a width to a large value (say 50%) and set Wrap="false".

<HeaderStyle Width="20%" Wrap="true" />
<ItemStyle Width="20%" Wrap="true" />
6

6 Answers

35
votes

Try This:

ItemStyle-Width="50%" ItemStyle-Wrap="false" in the BoundField tag

9
votes

For BoundField:

 <asp:BoundField DataField="UserName" HeaderText="User Name" ItemStyle-Width="50px" />
2
votes

It's amazing that even now, in 2016, the ItemStyle-Width and HeaderStyle-Width attributes usually get ignored in the ASP.Net GridView control.

Sometimes, they just seem to create no markup whatsoever.

My solution was to give up trying to set this attributes, and I resorted to using plain old CSS instead:

.AspNet-GridView table tbody tr td:nth-child(1)
{
    /*  Set the width of the 1st GridView column */
    width: 200px;
}
.AspNet-GridView table tbody tr td:nth-child(2)
{
    /*  Set the width of the 2nd GridView column */
    width: 300px;
}
0
votes

I am also facing this problem today. What i got is you must define ur width in css class & called that css class in boundfeild. e.g.

HeaderStyle-CssClass="width350"
0
votes

To change column width gridview boundfield just add this inside boundfield

ItemStyle-Width="200"  ItemStyle-Wrap="False"

it worked for me, try this

0
votes

After trying several solutions with no luck, I was able to add a css class to the item. Seems like both HeaderStyle-CssClass and ItemStyle-CssClass attributes needed to be set:

    <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-CssClass="Name" ItemStyle-CssClass="Name" />