Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid Column Width

Status
Not open for further replies.

wallaceoc80

Programmer
Jul 7, 2004
182
GB
Is there any way to set the width of each column separately for a DataGrid? I want the columns to have different set widths but at the moment the width of the columns is being deteremined by the content of the cells.

Thanks for your help,

Wallace
 
Just to let you know I think that part of the problem may be that some of the cells contain very long file names that come from the database, e.g.:

TMSSwiftMatches_yyyymmddhhmm.DAT


Is there any way I can get these to wrap on to the next line?

Regards,

Wallace
 
If you are using a BoundColumn for each column of data, set the HeaderStyle-Width property to your desired width.

Mark
 
Just tried that and unfortunately it does not work!

Here is my code, maybe you see a mistake:

Code:
<asp:datagrid id="dgFeeds" style="MARGIN-BOTTOM: 5px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 3px" BorderColor="#999999" BorderWidth="1px" BorderStyle="None" Width="450px" runat="server" 	AutoGenerateColumns="False" GridLines="Vertical" CellPadding="3" BackColor="White" HorizontalAlign="Center" OnEditCommand="dgFeeds_Edit" OnCancelCommand="dgFeeds_Cancel" OnUpdateCommand="dgFeeds_Update">
							<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" Wrap="False"></HeaderStyle>
							<FooterStyle ForeColor="Black" BackColor="#CCCCCC" Wrap="False"></FooterStyle>
							<Columns>
								<asp:BoundColumn DataField="FeedName" HeaderText="Feed Name" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:HyperLinkColumn DataNavigateUrlField="" DataNavigateUrlFormatString="WebForm2.aspx?page=AppDetails&amp;appid={0}" DataTextField="FromApp" HeaderText="From">
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:HyperLinkColumn>
								<asp:HyperLinkColumn DataNavigateUrlField="" DataNavigateUrlFormatString="WebForm2.aspx?page=AppDetails&amp;appid={0}" DataTextField="ToApp" HeaderText="To">
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:HyperLinkColumn>
								<asp:BoundColumn DataField="Directory" HeaderText="Directory" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="FileName" HeaderText="File Name" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="ArrivalTime" HeaderText="Arrival Time" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="Frequency" HeaderText="Frequency" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="CreationJob" HeaderText="Creation Job" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="Comments" HeaderText="Comments" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="Last Modified" HeaderText="Last Modified" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:BoundColumn DataField="Modified By" HeaderText="Modified By" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>
								<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="OK" />
							</Columns>
						</asp:datagrid>

Seen as that looks terrible I'll cut a piece out!
Code:
<asp:BoundColumn DataField="Modified By" HeaderText="Modified By" >
									<HeaderStyle HorizontalAlign="Center" Wrap="False" VerticalAlign="Middle" Width="40px"></HeaderStyle>
								</asp:BoundColumn>

Thanks for your help,

Wallace
 
HTML text will not wrap without a breaking space. It's an unfortunate reality of web design.

[sad]

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
An unfortunate and frustrating reality!!

I'm thinking of doing something like inserting a space in the string after say 25 characters and then when if I have to write it back to the database remove the space at this character.

Do you think this is a good idea? Can you think of anything better?

Regards,

Wallace
 
If you're using SqlServer as your datasource, then you could write a function that would do this string manipulation.

If you handled it this way, then you would select via this function, and your rowset would already be properly formatted when it came out w/o you having to change anything... except your sql statement, of course.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
So, you think I should do the string manipulation in my query as opposed to doing a loop through every row of the dataset and doing it that way?

Regards,

Wallace
 
Try using the HeaderStyle-Width property, not Width. I had the same problem until I switched and it worked for me.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top