I have a GridView which uses BoundField for columns. I am trying to set a maxwidth for my UserInfo
column.
I have tried many many ways but non of them work. Below is the code for my GridView :
<asp:GridView ID="GridView1" AutoGenerateEditButton="True"
ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="UserId"
DataField="UserId"
SortExpression="UserId"></asp:BoundField>
<asp:BoundField HeaderText="Username"
DataField="Username"
SortExpression="Username"></asp:BoundField>
<asp:BoundField HeaderText="UserInfo"
DataField="UserInfo"
SortExpression="UserInfo"></asp:BoundField>
</Columns>
</asp:GridView>
Looking for suggestions on how I can set the width of a specific column, which is my UserInfo
column.
<asp:GridView ID="GridView1" AutoGenerateEditButton="True"
ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" width="600px">
<Columns>
<asp:BoundField HeaderText="UserId"
DataField="UserId"
SortExpression="UserId" ItemStyle-Width="400px"></asp:BoundField>
</Columns>
</asp:GridView>
Width can be set to specific column as below: By percentages:
<asp:BoundField HeaderText="UserInfo" DataField="UserInfo"
SortExpression="UserInfo" ItemStyle-Width="100%"></asp:BoundField>
OR
By pixel:
<asp:BoundField HeaderText="UserInfo" DataField="UserInfo"
SortExpression="UserInfo" ItemStyle-Width="500px"></asp:BoundField>
This is going to work for all situations while working with width.
`<asp:TemplateField HeaderText="DATE">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("date") %>' Width="100px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>`
hey recently i figured it out how to set width if your gridview databound with sql dataset.first set these control RowStyle-Wrap="false" HeaderStyle-Wrap="false"
and then you can set the column width as much as you like. ex : ItemStyle-Width="150px" HeaderStyle-Width="150px"
add HeaderStyle in your bound field:
<asp:BoundField HeaderText="UserId"
DataField="UserId"
SortExpression="UserId">
<HeaderStyle Width="200px" />
</asp:BoundField>
Source: Stackoverflow.com