Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...