Set Width of GridView TextBox in Edit mode

To modify the width of a TextBox (inside the GridView control) when the GridView is in Edit Mode, use any of the two approaches:

Using ControlStyle

<asp:GridView ID="gvCustom" runat="server">
<
Columns>
<
asp:BoundField DataField="EmpID" HeaderText="EmployeeID" >
<
ControlStyle Width="150" />
</
asp:BoundField>
</Columns>
</
asp:GridView>

Use ControlStyle CSS Class (Recommended)

<style type="text/css">
.cssWdth
{
width: 150px;
}
</style>

and in the GridView

<asp:GridView ID="gvCustom" runat="server">
<
Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmployeeID"
ControlStyle-CssClass="cssWdth" />
</
Columns>
</
asp:GridView>

Check some additional GridView Tips and Tricks over here

5 comments:

  1. Thank you so much after spending an hour trying to figure out why my styles weren't working inside the gridview/detailview this fixed it!

    ReplyDelete
  2. Thanks a lot! It works fine with TemplateField as well.

    ReplyDelete
  3. Thank you. Useful for me.

    ReplyDelete
  4. Thank you. Very straight forward.

    ReplyDelete
  5. Thank You very much!
    outstaanding decision

    ReplyDelete