Question: Setting Datagrid's Column Width via Code

V

VB Programmer

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!
 
V

VB Programmer

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top