22

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" />
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
mmk_open
  • 1,005
  • 5
  • 18
  • 27

6 Answers6

36

Try This:

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

Saurabh
  • 5,661
  • 2
  • 26
  • 32
9

For BoundField:

 <asp:BoundField DataField="UserName" HeaderText="User Name" ItemStyle-Width="50px" />
thevan
  • 10,052
  • 53
  • 137
  • 202
2

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;
}
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
0

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

To change column width gridview boundfield just add this inside boundfield

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

it worked for me, try this

EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
0

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" />
Ben
  • 1,853
  • 19
  • 20