Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  Datagrid columns (Thursday, December 23, 2004)




Found the following interesting discussion in the Newsgroups:

datagrid columns
by:Lisa

Still trying to get rich text from my dataset into a datagrid. So far, my
strategy has been to derive a custom column style which owns a rich text
box:

Public Class RTColumn
Inherits DataGridColumnStyle

Dim WithEvents rtb As RichTextBox = New RichTextBox()
Dim PHeight As Integer = 0
Dim PSize As Size
Dim MHeight As Integer = 0
Dim ioMemoryStream As MemoryStream
Dim rtbg As Graphics
Dim mfMetaFile As Metafile
Dim ptrHDC As IntPtr

I want the rich text box with events, because I'd like to know the size of
the contents when the text is assigned to rtb:

Private Sub rtb_ContentsResized(ByVal sender As Object, ByVal e As
System.Windows.Forms.ContentsResizedEventArgs) Handles rtb.ContentsResized
If e.NewRectangle.Height > MHeight Then
MHeight = e.NewRectangle.Height
End If
If e.NewRectangle.Height > PHeight Then
PHeight = e.NewRectangle.Height
End If
PSize = e.NewRectangle.Size
End Sub

To draw the contents of the rtb, I create a metafile in the Paint override:
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
ByVal bounds As System.Drawing.Rectangle, ByVal [source] As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
ByVal alignToRight As Boolean)
Try
Dim rt As String = Me.GetColumnValueAtRow([source], rowNum)
g.FillRectangle(backBrush, bounds) 'this has the effect
of erasing the cell
If rt <> "" Then
rtb.Rtf = rt
ioMemoryStream = New MemoryStream()
' Get a graphics context from the RichTextBox
Dim rtbg As Graphics = rtb.CreateGraphics
' Get the device context from the graphics context
ptrHDC = rtbg.GetHdc()
' Create a new Enhanced Metafile from the device context
mfMetaFile = New Metafile(ioMemoryStream, ptrHDC)
' Release the device context
rtbg.ReleaseHdc(ptrHDC)
' Draw the metafile
g.DrawImage(mfMetaFile, bounds.X, bounds.Y, PSize.Width,
PSize.Height)

End If
Catch
MsgBox(Err.Description)
End Try
End Sub 'Paint

Not only does this fail to draw the rich text in the datagrid cell, but I
get a 'Cast from type 'DBNull' to type 'String' is not valid' error after
scrolling the grid to the last row.

I know about the dotnet.leadit.be/extendeddatagrid. It looks intruiging,
but I'd really rather understand what I'm doing, and not have to include
another dll with my project. If anyone can point out the error of my ways,
I'd very much appreciate it.




 Reply:
by:Ken Tucker [MVP]

 Lisa,

To avoid the cast from dbnull to string error use
getcolumnvalueatrow.tostring

Dim rt As String = Me.GetColumnValueAtRow([source], rowNum).tostring

Here is a link on how print a rich text box contents. Maybe the
graphics code will help.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_RichTextBox.asp

Ken


 Reply:
by:Lisa

 Ken,
Thanks - the .tostring was a dumb error on my part. And thanks for the
reference: I'll look at it tonight.








0 Comments:

Post a Comment

 
Previous Posts
    - XML?, INI File?, How do I do this?
    - character selection in a string
    - Split function not working.....
    - Multi-Lingual application
    - HasChanges not being updated?
    - VB6 conversion of UDT to structure - ValueType?
    - Deployment question
    - Can a VB.NET 2003 project work with framework 1.0?...
    - .Net Assembly via VBS
    - Need to include original DLL with Interop?

Archives
    - 10/03/2004 - 10/10/2004
    - 10/10/2004 - 10/17/2004
    - 10/17/2004 - 10/24/2004
    - 10/24/2004 - 10/31/2004
    - 10/31/2004 - 11/07/2004
    - 11/21/2004 - 11/28/2004
    - 11/28/2004 - 12/05/2004
    - 12/05/2004 - 12/12/2004
    - 12/12/2004 - 12/19/2004
    - 12/19/2004 - 12/26/2004
    - 12/26/2004 - 01/02/2005
    - 01/23/2005 - 01/30/2005
    - 01/01/2006 - 01/08/2006
    - 09/24/2006 - 10/01/2006

Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland