By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Question: Setting Datagrid's Column Width via Code

P: n/a
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!
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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!

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.