Datagrid question

S

Scott Meddows

I have a custom Datagrid that I want to do somthing with. When I resize a column I want a tooltip like box to come up and tell me
the width of the column. Is that hard to do? Posted is the code for what I currently have.

Thanks
Scott

Public Class MyDataGrid
Inherits DataGrid

Public Event MyVScroll As ScrollEventHandler
Public Event MyHScroll As ScrollEventHandler

Protected Overrides Sub GridVScrolled(ByVal sender As Object, ByVal e As ScrollEventArgs)
MyBase.GridVScrolled(Me, e)
OnMyVScroll(e)
End Sub

Protected Overrides Sub GridHScrolled(ByVal sender As Object, ByVal e As ScrollEventArgs)
MyBase.GridHScrolled(sender, e)
OnMyHScroll(e)
End Sub

Protected Sub OnMyVScroll(ByVal e As ScrollEventArgs)
RaiseEvent MyVScroll(Me, e)
End Sub

Protected Sub OnMyHScroll(ByVal e As ScrollEventArgs)
RaiseEvent MyHScroll(Me, e)
End Sub

Public Sub SynVScroll(ByVal e As ScrollEventArgs)
MyBase.GridVScrolled(Me, e)
End Sub

Public Sub SynHScroll(ByVal e As ScrollEventArgs)
MyBase.GridHScrolled(Me, e)
End Sub
End Class
 
K

Ken Tucker [MVP]

Hi,

Here is an example. I use the mouse move event to set the
tooltip text.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da As SqlDataAdapter

Dim conn As SqlConnection

Dim ds As New DataSet

strConn = "Server = (local);"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"



conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * From Customers", conn)

da.Fill(ds, "Customers")

SetupGrid()

DataGrid1.DataSource = ds.Tables("Customers")

End Sub

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "Customers"



Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "ContactName"

..HeaderText = "Name"

..Width = 150

End With

Dim colID As New DataGridTextBoxColumn

With colID

..MappingName = "CustomerID"

..HeaderText = "ID"

..Width = 80

End With

Dim colRegion As New DataGridTextBoxColumn

With colRegion

..MappingName = "Region"

..HeaderText = "Region"

..Width = 80

..NullText = ""

End With



ts.GridColumnStyles.Add(colID)

ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colRegion)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colRegion = Nothing

colName = Nothing

colID = Nothing

End Sub



Private Sub DataGrid1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseMove

Dim hti As DataGrid.HitTestInfo

hti = DataGrid1.HitTest(e.X, e.Y)

If hti.Type = DataGrid.HitTestType.ColumnResize Then

Dim strOut As String

strOut = String.Format("Column Width {0}",
DataGrid1.TableStyles(0).GridColumnStyles.Item(hti.Column).Width)

ToolTip1.SetToolTip(DataGrid1, strOut)

End If

End Sub



Ken

---------------------------------

I have a custom Datagrid that I want to do somthing with. When I resize a
column I want a tooltip like box to come up and tell me
the width of the column. Is that hard to do? Posted is the code for what
I currently have.

Thanks
Scott

Public Class MyDataGrid
Inherits DataGrid

Public Event MyVScroll As ScrollEventHandler
Public Event MyHScroll As ScrollEventHandler

Protected Overrides Sub GridVScrolled(ByVal sender As Object, ByVal e As
ScrollEventArgs)
MyBase.GridVScrolled(Me, e)
OnMyVScroll(e)
End Sub

Protected Overrides Sub GridHScrolled(ByVal sender As Object, ByVal e As
ScrollEventArgs)
MyBase.GridHScrolled(sender, e)
OnMyHScroll(e)
End Sub

Protected Sub OnMyVScroll(ByVal e As ScrollEventArgs)
RaiseEvent MyVScroll(Me, e)
End Sub

Protected Sub OnMyHScroll(ByVal e As ScrollEventArgs)
RaiseEvent MyHScroll(Me, e)
End Sub

Public Sub SynVScroll(ByVal e As ScrollEventArgs)
MyBase.GridVScrolled(Me, e)
End Sub

Public Sub SynHScroll(ByVal e As ScrollEventArgs)
MyBase.GridHScrolled(Me, e)
End Sub
End Class
 
S

Scott Meddows

Well, I found a VERY SIMPLE solution to this (As is usually the case in .Net) and I thought I would post this in case someone else
was looking for this solution.

There is a WidthChanged Event on the TextBoxDatagridColumn object that I just capture and then sync the datagrids.

IE

Private Sub TopToBottom_WidthChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles NameColumn.WidthChanged,
SourceColumn.WidthChanged, PayerDescColumn.WidthChanged, Reached90Column.WidthChanged
PreviousNameColumn.Width = NameColumn.Width
PreviousSourceColumn.Width = SourceColumn.Width
PreviousPayerDescColumn.Width = PayerDescColumn.Width
PreviousReached90Column.Width = Reached90Column.Width
End Sub

Private Sub BottomToTop_WidthChange(ByVal sender As Object, ByVal e As EventArgs) Handles PreviousNameColumn.WidthChanged,
PreviousSourceColumn.WidthChanged, PreviousPayerDescColumn.WidthChanged, PreviousReached90Column.WidthChanged
NameColumn.Width = PreviousNameColumn.Width
SourceColumn.Width = PreviousSourceColumn.Width
PayerDescColumn.Width = PreviousPayerDescColumn.Width
Reached90Column.Width = PreviousReached90Column.Width
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top