473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

trimming text in a datagrid to match column length

I have a messaging application that has a data grid with information like an
email list would have (from, subject, time sent, size) but the subject
could be very long in theory, and then it would word wrap.. the subject is a
variable width column (resizes to fit space) while the others are fixed
size... the problem is that i dont want the text to ever word wrap.. but
truncate the visible subject to match the current cell width... (kinda like
gmail does) how would i go about doing this at runtime on the client's page?
thanks!

Nov 18 '05 #1
5 3485
I guess there are a couple of ways that I can think of to fix this. The first and easiest way could be to only return a certain number of characters from the query of stored procedure. For example:

select substring(subject, 0, 10) from emaillist

that way the query will do all the work.

Now another example could be to do it within the code, for example:

<ItemTemplate>
<%# TrimSubject(DataBinder.Eval(Container.DataItem, "Subject")) %>
</ItemTemplate>

public string TrimSubject(string subject)
{
string retVal = subject;
if(retVal.Length>100)
{
retVal = retVal.Substring(0, 100) + "...";
}

return retVal;
}

Hope those examples help.

Alan Washington
http://www.aewnet.com
I have a messaging application that has a data grid with information like an
email list would have (from, subject, time sent, size) but the subject
could be very long in theory, and then it would word wrap.. the subject is a
variable width column (resizes to fit space) while the others are fixed
size... the problem is that i dont want the text to ever word wrap.. but
truncate the visible subject to match the current cell width... (kinda like
gmail does) how would i go about doing this at runtime on the client's page?
thanks!


User submitted from AEWNET (http://www.aewnet.com/)
Nov 18 '05 #2
You can use GDI+ to do this. Here's some quick/dirty code I wrote to do
this. Hope this helps!

Joel Cade, MCSD
Fig Tree Solutions, LLC
http://www.figtreesolutions.com

Imports System.Drawing

Public Function MeasureString(ByVal Value As String, ByVal Font As
Drawing.Font) As SizeF
' Set up string.
Dim oBitMap As New Bitmap(1, 1)
Dim oGraphics As Graphics = Graphics.FromImage(oBitMap)

' Measure string.
Dim Size As New SizeF
Size = oGraphics.MeasureString(Value, Font)

Return Size
End Function

Public Function GetTrimmedString(ByVal Value As String, ByVal MaxSizeF
As Single, ByVal Font As Drawing.Font) As String
Dim sReturn As String

If MeasureString(Value, Font).Width < MaxSizeF Then
sReturn = Value
Else
sReturn = Value

Dim i As Integer

Do While MeasureString(Value.Substring(0, Value.Length - i) &
"...", Font).Width > MaxSizeF
i += 1
Loop

sReturn = Value.Substring(0, Value.Length - i) & "..."
End If

Return sReturn
End Function

......

ListBox1.Items.Add(oFormatter.GetTrimmedString("Th is is a test, THIS IS THE
LONGEST EVER THIS IS THE LONGEST EVER THIS IS THE LONGEST EVER THIS IS THE
LONGEST EVER", 300, New Font(New
System.Drawing.FontFamily(ListBox1.Font.Name), 12, FontStyle.Regular)))

Nov 18 '05 #3
GDI+ on asp.net on the client side? doesn't sound right, because the user
could resize the window thus changing the size of the column the text is in.
"Joel Cade" <jo**@nospam.figtreesolutions.com> wrote in message
news:0F**********************************@microsof t.com...
You can use GDI+ to do this. Here's some quick/dirty code I wrote to do
this. Hope this helps!

Joel Cade, MCSD
Fig Tree Solutions, LLC
http://www.figtreesolutions.com

Imports System.Drawing

Public Function MeasureString(ByVal Value As String, ByVal Font As
Drawing.Font) As SizeF
' Set up string.
Dim oBitMap As New Bitmap(1, 1)
Dim oGraphics As Graphics = Graphics.FromImage(oBitMap)

' Measure string.
Dim Size As New SizeF
Size = oGraphics.MeasureString(Value, Font)

Return Size
End Function

Public Function GetTrimmedString(ByVal Value As String, ByVal MaxSizeF
As Single, ByVal Font As Drawing.Font) As String
Dim sReturn As String

If MeasureString(Value, Font).Width < MaxSizeF Then
sReturn = Value
Else
sReturn = Value

Dim i As Integer

Do While MeasureString(Value.Substring(0, Value.Length - i) &
"...", Font).Width > MaxSizeF
i += 1
Loop

sReturn = Value.Substring(0, Value.Length - i) & "..."
End If

Return sReturn
End Function

.....

ListBox1.Items.Add(oFormatter.GetTrimmedString("Th is is a test, THIS IS
THE
LONGEST EVER THIS IS THE LONGEST EVER THIS IS THE LONGEST EVER THIS IS THE
LONGEST EVER", 300, New Font(New
System.Drawing.FontFamily(ListBox1.Font.Name), 12, FontStyle.Regular)))

Nov 18 '05 #4
Hi Brian,

As for the truncating string values in DataGrid Cell(Table cell), I think
generally we have two means:

1. Truncating the string's length at serverside when we retrieve it from db
or output into the page via code. Just as the other members have suggested.
But this will make the output string's length be a fixed size.

#Creating a Custom DataGridColumn Class, Part 2
http://aspnet.4guysfromrolla.com/art...00202-1.2.aspx

2. Using css style on the output <table> and the text content in table
cell. There is one css attribute named
"OVERFLOW", it can be applied on <div> or <p> ... and when we set the
"OVERFLOW:Hidden", it will truncat the exceeded content according to the
container element's width.

#overflow Attribute | overflow Property
http://msdn.microsoft.com/library/de...thor/dhtml/ref
erence/properties/overflow.asp

For example:

<p style="OVERFLOW:hidden;WIDTH:100">
<% Response.Write(new String('d',1000)); %>
</p>

Also, since in our problem, we need to set the datagrid column's width
unfixed, so we also need to make the with of the <p> as a relative size.
After some testing, it seems that we also have to assign the "table-layout
: fixed" css attribute to the DataGrid(<table>). Here is some demo code on
applying the styles on datagrid:

<asp:DataGrid id="dgStyle" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Column1"
HeaderText="Column1"></asp:BoundColumn>
<asp:BoundColumn DataField="Column2"
HeaderText="Column2"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Column3">
<ItemTemplate>
<p style="OVERFLOW:hidden;WIDTH:90%">
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Column3") %>'>
</p>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

=======================
In our code-behind , we can use the following code (in Page_load) to apply
the "table-layout" style on datagrid:

private void Page_Load(object sender, System.EventArgs e)
{
....................

dgStyle.Style["TABLE-LAYOUT"] = "fixed";
}
Hope these help. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #5
thanks that worked perfectly!

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:%2****************@cpmsftngxa10.phx.gbl...
Hi Brian,

As for the truncating string values in DataGrid Cell(Table cell), I think
generally we have two means:

1. Truncating the string's length at serverside when we retrieve it from
db
or output into the page via code. Just as the other members have
suggested.
But this will make the output string's length be a fixed size.

#Creating a Custom DataGridColumn Class, Part 2
http://aspnet.4guysfromrolla.com/art...00202-1.2.aspx

2. Using css style on the output <table> and the text content in table
cell. There is one css attribute named
"OVERFLOW", it can be applied on <div> or <p> ... and when we set the
"OVERFLOW:Hidden", it will truncat the exceeded content according to the
container element's width.

#overflow Attribute | overflow Property
http://msdn.microsoft.com/library/de...thor/dhtml/ref
erence/properties/overflow.asp

For example:

<p style="OVERFLOW:hidden;WIDTH:100">
<% Response.Write(new String('d',1000)); %>
</p>

Also, since in our problem, we need to set the datagrid column's width
unfixed, so we also need to make the with of the <p> as a relative size.
After some testing, it seems that we also have to assign the "table-layout
: fixed" css attribute to the DataGrid(<table>). Here is some demo code
on
applying the styles on datagrid:

<asp:DataGrid id="dgStyle" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Column1"
HeaderText="Column1"></asp:BoundColumn>
<asp:BoundColumn DataField="Column2"
HeaderText="Column2"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Column3">
<ItemTemplate>
<p style="OVERFLOW:hidden;WIDTH:90%">
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Column3") %>'>
</p>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

=======================
In our code-behind , we can use the following code (in Page_load) to apply
the "table-layout" style on datagrid:

private void Page_Load(object sender, System.EventArgs e)
{
...................

dgStyle.Style["TABLE-LAYOUT"] = "fixed";
}
Hope these help. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PeterZ | last post by:
Hi, In a running C# app with a datagrid control I select all rows in the dataGrid using CTRL-A, I then paste into some other app like notepad or Word but the column headings get left off. Is...
0
by: Frnak McKenney | last post by:
One of the reasons given for the Allied victory in WWI is that the Nazi armament industry invested so much effort in creating new weapons (e.g. the jet plane) it wasn't able to develop any of them...
4
by: Mike | last post by:
Hi, Is there a possibility to have one of the Web Control Datagrid's column as a Calendar when editing data? Any resources on this subject? Thanks Mike
0
by: Jeremiah Adams | last post by:
I am having two issues with the Datagrid and I hope that someone can help me out with them. 1) Printing. I pulled the code for sending the control to a printer off the msdn site. It works for...
6
by: Aaron Smith | last post by:
Is there a way to put a limit on the text size of a datagrid column? Thanks, Aaron -- --- Aaron Smith Remove -1- to E-Mail me. Spam Sucks.
2
by: Jon Lapham | last post by:
I have a table that stores TEXT information. I need query this table to find *exact* matches to the TEXT... no regular expressions, no LIKE queries, etc. The TEXT could be from 1 to 10000+...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
2
by: cj | last post by:
I was looking over some of my 2003 code today (see below) that loads a foxpro table via oledb connection. I used a sub "autosizecolumns" I found on the web but I never quite understood why they...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.