Question: Setting Datagrid's Column Width via Code

Discussion in 'ASP .Net' started by VB Programmer, Aug 4, 2004.

  1. I have a datagrid which is bound to a datatable that I create on the fly.

    How do I set the width of each column via code?

    Ex: Column 1 is always 100, Column 2 is always 250, etc...

    Thanks!
     
    VB Programmer, Aug 4, 2004
    #1
    1. Advertising

  2. This worked....

    Set the DataSource and DataMember properties of the dg. (DataSource is a
    public var I create on the fly in code.)
    Unchecked "Create columns automatically at run time" and added 8 bound
    columns and set the Header Text and Data Field entries.
    Here's the HTML:

    <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#999999"
    BorderStyle="None" BorderWidth="1px"
    BackColor="White" CellPadding="3" GridLines="Vertical" Width="888px"
    Height="96px" DataSource="<%# ds %>" DataMember="WindFields"
    AutoGenerateColumns="False" Font-Names="Verdana" Font-Size="8pt">
    <SelectedItemStyle Font-Bold="True" ForeColor="White"
    BackColor="#008A8C"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
    <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White"
    BackColor="#000084"></HeaderStyle>
    <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
    <Columns>
    <asp:BoundColumn DataField="Location" HeaderText="Location">
    <HeaderStyle HorizontalAlign="Center" Width="150px"
    VerticalAlign="Middle"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Max Wind (mph)" HeaderText="Max Wind
    (mph)">
    <HeaderStyle HorizontalAlign="Center" Width="75px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="CPA (Miles)" HeaderText="CPA (Miles)">
    <HeaderStyle HorizontalAlign="Center" Width="50px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Wind Cycle (Knots)" HeaderText="Wind Cycle
    (Knots)">
    <HeaderStyle HorizontalAlign="Center" Width="50px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Arrival (Hrs)" HeaderText="Arrival (Hrs)">
    <HeaderStyle HorizontalAlign="Center" Width="50px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Date &amp; Time of Arrival"
    HeaderText="Date &amp; Time of Arrival">
    <HeaderStyle HorizontalAlign="Center" Width="300px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Date &amp; Time of Departure"
    HeaderText="Date &amp; Time of Departure">
    <HeaderStyle HorizontalAlign="Center" Width="300px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="Duration (hrs)" HeaderText="Duration
    (hrs)">
    <HeaderStyle HorizontalAlign="Center" Width="50px"
    VerticalAlign="Middle"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center"
    VerticalAlign="Middle"></ItemStyle>
    </asp:BoundColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="Black"
    BackColor="#999999" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>

    "VB Programmer" <Dont*NoSpam-Please*@jEmail.com> wrote in message
    news:%...
    > I have a datagrid which is bound to a datatable that I create on the fly.
    >
    > How do I set the width of each column via code?
    >
    > Ex: Column 1 is always 100, Column 2 is always 250, etc...
    >
    > Thanks!
    >
    >
     
    VB Programmer, Aug 4, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    783
    Bruce Barker
    Oct 10, 2005
  2. =?Utf-8?B?Um9iZXJ0IFcu?=

    Newbie Question About Setting Table Column Width

    =?Utf-8?B?Um9iZXJ0IFcu?=, Mar 31, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    527
    S. Justin Gengo
    Mar 31, 2006
  3. Charleees
    Replies:
    2
    Views:
    8,214
    Eliyahu Goldin
    Jun 20, 2006
  4. ssk
    Replies:
    5
    Views:
    5,513
    Jukka K. Korpela
    Oct 30, 2006
  5. Carl Howarth

    DG Column Width vs Table Column Width

    Carl Howarth, Oct 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    267
    Eliyahu Goldin
    Oct 24, 2004
Loading...

Share This Page