set column width of a gridview in asp.net

Vikas Kunte picture Vikas Kunte · Apr 20, 2011 · Viewed 104.3k times · Source

I dragged and dropped a GridView from toolbox to Aspx page.the design code looks like this

<asp:GridView ID="gridview1" runat="server"   style="text-align:center;width: 1327px;" 
    CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" 
    EnableSortingAndPagingCallbacks="True" PageSize="50" AutoGenerateEditButton="True">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" 
        Height="12px" VerticalAlign="Bottom" Width="12px" Wrap="False" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

I have a column by name Address in database.The table is bound to this grid view using following VB.NET code

 Dim ds as Data.DataSet
 Dim da As New OleDbDataAdapter("select col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,Address from table, con)
        da.Fill(ds)
        gridviewrealestate.DataSource = ds
        gridviewrealestate.DataBind()

Now,When the gridview is displayed,the columns are very close to each other due to large information present in Address Column.I want to set the Address Column to a particular width. Please help me how to do this.

Answer

rahul picture rahul · Jun 7, 2012
<asp:GridView ID="GridView1" runat="server">
    <HeaderStyle Width="10%" />
    <RowStyle Width="10%" />
    <FooterStyle Width="10%" />
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="LastName" 
            HeaderStyle-Width="10%" ItemStyle-Width="10%"
            FooterStyle-Width="10%" />
    </Columns>
</asp:GridView>