MeasureString problem

calvin

Regular
Joined
Nov 6, 2003
Messages
71
Hi,

I want to check the width of string in each row of particuluar column of datagrid. For example, there is a column binding student name and the column width is set to 170 px. The default font size is set to 8 point. Some of the student name exceed the length of row and it is wrapped. I want the system check width of student name on each row. So if it is exceed 170px, system will deduct the size of font until it is fit to the row width(something like shrink).

I search a lot of articles and forum also suggest use "MeasureString"., but I face some error.
Below is my code to check the string width:

For i As Integer = 0 To DataGrid.Items.Count - 1
Dim intFontSize As Integer = 8
Dim intColumnWidth As Integer = 170 'px
Dim intGetStringWidth As Integer

Dim item As DataGridItem = DataGrid.Items(i)

Dim measureString As String = item.Cells(2).Text 'Student Name
Dim stringFont As New Font("Arial", intFontSize)

Dim g As Graphics = Me.CreateGraphics <- Error 1
Dim stringSize As Double
stringSize = g.Graphics.MeasureString(measureString, stringFont) <-Error 2
While stringSize > intColumnWidth
intFontSize -= 1
stringFont = New Font("Arial", intFontSize)
g = Me.CreateGraphics <- Error 1
stringSize = g.Graphics.MeasureString(measureString, stringFont) <-Error 2

End While

item.Cells(2).Font.Size = FontUnit.Point(intFontSize)
item.Cells(2).ForeColor = Color.Red

Next


Error 1 Message:
CreateGraphics is not member of 'XXX.XXX'

Error 2 Message:
'Graphics is not member of 'System.Drawing.Graphics'

Anyone can help me!! Appreciate for any help and comments.

Calvin
 
This is ASP.NET, there is no me.Graphics.
This is no windows programm, and the width of a string depends on the Browser
displaying your HTML-Code.
 
Back
Top