473,387 Members | 1,897 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,387 software developers and data experts.

datagrid columns

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.


Nov 20 '05 #1
2 1167
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/de...ichTextBox.asp

Ken
---------------------

"Lisa" <sp****@herplace.com> wrote in message
news:Cc********************@eatel.net...
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.

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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:ek****************@tk2msftngp13.phx.gbl...
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/de...ichTextBox.asp
Ken
---------------------

"Lisa" <sp****@herplace.com> wrote in message
news:Cc********************@eatel.net...
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.


Nov 20 '05 #3

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

Similar topics

1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.