Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2006

Tip: Looking for answers? Try searching our database.

Forced Column Widths in a DataGrid?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex Maghen - 16 Jun 2006 15:32 GMT
Hi. Is there any way to lock down the width of columns in a DataGrid so that
even if the text in one of the rows of that column is very wide, it just gets
cut off or something rather than causing the column to get wider?

The problem is that even if I set the column to wrap text, sometimes a
single "word" or string is very long without any spaces and therefore doesn't
wrap but just forces the column wider - and that's not what I want.

Any ideas?

PS: I've tried to go the the DataGrid managed group a bunch of times over
the past month and, at least on the MSDN web site, it always shows as empty
with no items. That's why I'm posting this here instead of there.

Thanks!

Alex
bruce barker (sqlwork.com) - 16 Jun 2006 18:04 GMT
if you read CSS documentation, you will see you can set the overflow
behavior, which is what you want to do.

-- bruce (sqlwork.com)

> Hi. Is there any way to lock down the width of columns in a DataGrid so
> that
[quoted text clipped - 17 lines]
>
> Alex
Greg9Strat - 16 Jun 2006 18:06 GMT
Yes, you can using something like the following in your control:

<Columns>
   <asp:BoundField HeaderText="My Header" DataField="MyField"
HeaderStyle-Width="90px" />
</Columns>

That should force the column to be the width that you desire - by using
a unit measure of "px"... you can also use % to make it adjust to a
percentage of the window width...
Walter Wang [MSFT] - 20 Jun 2006 13:16 GMT
Hi Alex,

Thank you for your post.

You may need to set the table-layout to 'fixed' to make sure fixed width
column working. For example:

   protected void Page_Load(object sender, EventArgs e)
   {
       ArrayList ar = new ArrayList();
       ar.Add("ReallylonglinewithNoSpacesAtAll!!!!!!!!!! and
anotherverylonglinelonglinelongline");
       grid1.DataSource = ar;
       grid1.DataBind();
       grid1.Style.Add("table-layout", "fixed");
   }

   protected void grid1_ItemDataBound(object sender, DataGridItemEventArgs
e)
   {
       //e.Item.Cells[0].Text = "<nobr>" + e.Item.Cells[0].Text +
"</nobr>";
       e.Item.Cells[0].Attributes.Add("WIDTH", "75px");
       e.Item.Cells[0].Attributes.Add("onmouseover",
"this.title=this.innerText;");   
   }

This will show a table cell that has 3 lines but the two long words gets
clipped. If you don't want it gets wrapped, uncomment the first statement
in ItemDataBound and only the first word gets shown and clipped.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.