473,837 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid resizing rows on sort - not wanted

I successfully autosized the columns and rows on my Datagrid, and am
now facing another issue. Having the sorting ability by clicking the
column headers is key, but when I do that, it resizes all my rows back
to their defaults. I populate the datagrid, resize it accordingly and
it works fine...just when I click the headers is when it messes up. It
would be fine if I could run my autosizing again after the sorting is
done since there won't be more than 50 rows usually, but I haven't been
able to figure that out.

I know when the user clicks on a column header, but when I run the
autosizing on the mouseup of that click, it just doesnt take. I see
the datagrid flicker with the right row heights, but it just always
goes right back to default. Any ideas on how to keep all the rows the
same size when a user clicks the column heading for sorting?

Nov 21 '05 #1
4 2731
Hi,

When you sort the datagrid sets the row height back to the default
row height. They way I get around it is that I set the default row height
for all the rows to the biggest height and manually resize all the rows.

Imports System.Drawing

Imports System.Drawing. Drawing2D

Imports System.Windows. Forms

Imports System.Reflecti on

Imports System.Componen tModel

<DataSysDescrip tion("This column style displays more than one line of
text")> _

Public Class MultiLineColumn

Inherits HotTrackTextBox Column

Private mTxtAlign As HorizontalAlign ment

Private mDrawTxt As New StringFormat

Private mbAdjustHeight As Boolean = True

Private m_intPreEditHei ght As Integer

Private m_rownum As Integer

Dim WithEvents dg As DataGrid

Private arHeights As ArrayList

Dim WithEvents cm As CurrencyManager

Dim mbTrack As Boolean = True

Public Property HotTrack() As Boolean

Get

Return mbTrack

End Get

Set(ByVal Value As Boolean)

mbTrack = Value

End Set

End Property

Private Sub GetHeightList()

Dim mi As MethodInfo = dg.GetType().Ge tMethod("get_Da taGridRows",
BindingFlags.Fl attenHierarchy Or BindingFlags.Ig noreCase Or
BindingFlags.In stance Or BindingFlags.No nPublic Or BindingFlags.Pu blic Or
BindingFlags.St atic)

Dim dgra As Array = CType(mi.Invoke (Me.dg, Nothing), Array)

arHeights = New ArrayList

Dim dgRowHeight As Object

For Each dgRowHeight In dgra

If dgRowHeight.ToS tring().EndsWit h("DataGridRela tionshipRow") = True Then

arHeights.Add(d gRowHeight)

End If

Next

End Sub

Public Sub New()

mTxtAlign = HorizontalAlign ment.Left

mDrawTxt.Alignm ent = StringAlignment .Near

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(sou rce, rowNum, bounds, [readOnly], instantText, cellIsVisible)

Me.TextBox.Text Align = mTxtAlign

Me.TextBox.Mult iline = mbAdjustHeight

Debug.WriteLine (rowNum)

'If rowNum >= iRows Then

For x As Integer = 0 To arHeights.Count - 1

Dim pi As PropertyInfo = arHeights(x).Ge tType().GetProp erty("Height")

Dim curHeight As Integer = CInt(pi.GetValu e(arHeights(x), Nothing))

pi.SetValue(arH eights(x), curHeight, Nothing)

Next

Dim sz As Size = dg.Size

dg.Size = New Size(sz.Width - 1, sz.Height - 1)

dg.Size = sz

GetHeightList()

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics,
ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

If Not bPainted Then

MyBase.Paint(g, bounds, source, rowNum, foreBrush, backBrush, alignToRight)

dg = Me.DataGridTabl eStyle.DataGrid

GetHeightList()

End If

cm = source

'clear the cell

g.FillRectangle (backBrush, bounds)

'draw the value

Dim s As String = Me.GetColumnVal ueAtRow([source], rowNum).ToStrin g()

Dim r As New RectangleF(boun ds.X, bounds.Y, bounds.Width, bounds.Height)

r.Inflate(-2, -2)

' get the height column should be

Dim sDraw As SizeF = g.MeasureString (s, Me.TextBox.Font , Me.Width, mDrawTxt)

Dim h As Integer = CInt(sDraw.Heig ht) + 15

If mbAdjustHeight Then

AdjustHeight(h)

End If

g.DrawString(s, MyBase.TextBox. Font, foreBrush, r, mDrawTxt)

bPainted = True

If mbTrack = True And Me.MouseOverCel l(rowNum) Then

g.DrawRectangle (New Pen(SystemColor s.HotTrack), r.Left, r.Top, r.Width,
r.Height)

End If

End Sub

<DataSysDescrip tion("Automatic ally increase height of column")> _

Public Property AutoAdjustHeigh t() As Boolean

Get

Return mbAdjustHeight

End Get

Set(ByVal Value As Boolean)

mbAdjustHeight = Value

Try

dg.Invalidate()

Catch

End Try

End Set

End Property

Private Sub cm_PositionChan ged(ByVal sender As Object, ByVal e As
System.EventArg s) Handles cm.PositionChan ged

Static intOld As Integer = 0

If cm.Count > DirectCast(dg.D ataSource, DataTable).Rows .Count Then

For x As Integer = 0 To arHeights.Count - 1

Dim pi As PropertyInfo = arHeights(x).Ge tType().GetProp erty("Height")

Dim curHeight As Integer = CInt(pi.GetValu e(arHeights(x), Nothing))

pi.SetValue(arH eights(x), curHeight, Nothing)

Next

Dim sz As Size = dg.Size

dg.Size = New Size(sz.Width - 1, sz.Height - 1)

dg.Size = sz

End If

intOld = cm.Position

End Sub

Private Sub AdjustHeight(By Val h As Integer)

Dim curHeight As Integer = Me.DataGridTabl eStyle.Preferre dRowHeight

If h > curHeight Then

'

' Manually set all the row heights to the new height

'

Me.DataGridTabl eStyle.Preferre dRowHeight = h

Trace.WriteLine (h)

If arHeights.Count < cm.Count Then

GetHeightList()

End If

For rownum As Integer = 0 To cm.Count - 1

Try

Dim pi As PropertyInfo

pi = arHeights(rownu m).GetType().Ge tProperty("Heig ht")

' adjust height

If h > curHeight Then

pi.SetValue(arH eights(rownum), h, Nothing)

End If

Catch

' something wrong leave default height

Debug.WriteLine ("Error")

End Try

Next

'

' Resize datagrid to get scrollbars to work right

'

Dim sz As Size = dg.Size

dg.Size = New Size(New Point(sz.Width - 1, sz.Height - 1))

dg.Size = sz

End If

End Sub

<DataSysDescrip tion("Suspends painting of column until EndUpdate is
called")> _

Public Shadows Sub BeginUpdate()

MyBase.BeginUpd ate()

End Sub

<DataSysDescrip tion("Resumes painting of column")> _

Public Shadows Sub EndUpdate()

MyBase.EndUpdat e()

End Sub

End Class

Ken

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

"skOOb33" <sk*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I successfully autosized the columns and rows on my Datagrid, and am
now facing another issue. Having the sorting ability by clicking the
column headers is key, but when I do that, it resizes all my rows back
to their defaults. I populate the datagrid, resize it accordingly and
it works fine...just when I click the headers is when it messes up. It
would be fine if I could run my autosizing again after the sorting is
done since there won't be more than 50 rows usually, but I haven't been
able to figure that out.

I know when the user clicks on a column header, but when I run the
autosizing on the mouseup of that click, it just doesnt take. I see
the datagrid flicker with the right row heights, but it just always
goes right back to default. Any ideas on how to keep all the rows the
same size when a user clicks the column heading for sorting?
Nov 21 '05 #2
A couple questions. Can that class just inherit the
DataGridTextBox Column, or just the HotTrackTextBox Column? Also, there
seems to be no function named MouseOverCell() given, but is used in the
Paint method. Any clarification is appreciated. Thanks.

Nov 21 '05 #3
Hi,

Sorry you can use datagridtextbox column instead of the
hottracktextbox column and get rid of the line with mouseovercell. But here
is Hottracktextbox column in case you want it. It will highlight the cell
the mouse is over.
Imports System.Drawing

Imports System.Drawing. Drawing2D

Imports System.Windows. Forms

Imports System.Reflecti on

Imports System.Componen tModel

Public Class HotTrackTextBox Column

Inherits DataGridTextBox Column

Dim c As Integer

Dim rectPaint As New RectangleF

Dim fnt As New Font(MyBase.Tex tBox.Font.Name, MyBase.TextBox. Font.Size,
FontStyle.Under line)

Dim WithEvents dg As DataGrid

Dim oldCell As New Point(-1, -1)

Dim isInCell As Boolean = False

Public Sub HandleMouseMove (ByVal sender As Object, ByVal e As
MouseEventArgs) Handles dg.MouseMove

Dim g As Graphics = dg.CreateGraphi cs

Dim bounds As Rectangle

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(New Point(e.X, e.Y))

isInCell = (hti.Row > -1 And hti.Column = c)

Static bRedraw As Boolean = False

If isInCell Then

Dim pt As Point

pt = New Point(hti.Row, hti.Column)

If Not pt.Equals(oldCe ll) Then

'

' Create a region where we want the datagrid to redraw

' So the datagrid doesn't flash.

'

Dim pthToRedraw As New Drawing2D.Graph icsPath

Dim rgnToRedraw As Region

Dim rCell As Rectangle = dg.GetCellBound s(pt.X, pt.Y)

pthToRedraw.Add Rectangle(rCell )

If oldCell.X > -1 Then

'

' Have to redraw last cell

'

pthToRedraw.Add Rectangle(dg.Ge tCellBounds(old Cell.X, oldCell.Y))

End If

rgnToRedraw = New Region(pthToRed raw)

dg.Invalidate(r gnToRedraw)

End If

'

' Flag datagrid for redraw

'

bRedraw = True

oldCell = pt

Else

'

' Only redraw when needed

'

If bRedraw Then

dg.Invalidate(d g.GetCellBounds (oldCell.X, oldCell.Y))

End If

bRedraw = False

oldCell = New Point(-1, -1)

End If

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics,
ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

'

' First time we paint get a reference to datagrid

' So we can consume its events

'

If Not bPainted Then

dg = Me.DataGridTabl eStyle.DataGrid

c = -1

For Each grdCol As DataGridColumnS tyle In
Me.DataGridTabl eStyle.GridColu mnStyles

c += 1

If grdCol.MappingN ame = Me.MappingName Then Exit For

Next

End If

bPainted = True

MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)

Dim pnHot As New Pen(SystemColor s.HotTrack)

Dim brHot As New SolidBrush(Colo r.FromArgb(128, SystemColors.Ho tTrack))

' see through brush

If MouseOverCell(r owNum) Then

bounds.Inflate(-1, -1)

g.FillRectangle (brHot, bounds)

g.DrawRectangle (pnHot, bounds)

End If

Dim r As New RectangleF(boun ds.X, bounds.Y, bounds.Width, bounds.Height)

rectPaint = r

End Sub

Public ReadOnly Property MouseOverCell(B yVal rownum As Integer) As Boolean

Get

Dim pt As New Point(rownum, c)

Return pt.Equals(oldCe ll)

End Get

End Property

End Class

Ken

---------------------
"skOOb33" <sk*****@gmail. com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
A couple questions. Can that class just inherit the
DataGridTextBox Column, or just the HotTrackTextBox Column? Also, there
seems to be no function named MouseOverCell() given, but is used in the
Paint method. Any clarification is appreciated. Thanks.
Nov 21 '05 #4
Thanks. All I did to rectify the issue was set a timer for 1/2 a
second to resize the rows after the sort. It isn't as good as I wanted
because there is a slight flicker after you sort. I wasn't able to
resize at the right time/event without the timer. I put your HotTrack
to use, and it's a pretty cool feature. Probably won't fly with the
"higher-ups" for now, but probably with one of my next projects.
Thanks. If you have any ideas on a seemless autosizing after sorting,
I would be interested.

thx

Nov 21 '05 #5

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

Similar topics

3
539
by: sam | last post by:
Hello Group, Havent had luck posting it to microsoft.public.dotnet.framework.aspnet.datagridcontrol group. Excuse me for the cross posting. I have a datagrid which needs to be split into multiple pages when printing along with headers on every page. I came up with this routine below which adds 2 rows - one with datagrid headers and the other is also a headers which consists other related information. The 2 rows(headers) needs to be...
0
1851
by: Tom Hughes | last post by:
I want to change one field of all selected rows to a provided value. Problem 1 I am using the Binding Manager Base to bind the datagrid to the appropriate dataTable as recommended by KB817247. It works in all cases but one. If the field being changed is also one the user has selected as a sort column, then all but the first selections are missed. If I remove the sort from that column before or after
1
4186
by: Digit | last post by:
Hi, I've got a problem. (why else should I post ;) ) I'ce created a datagrid and I want some rows always on top even when the user sorts a column. This I did by creating a new column and hide it. I gave the rows I wanted on top lower values then the rest (used default values). Then I turned off the AllowSorting property. When the user clicks on a columnheader I sort on the hidden column as first column and the second column the "clicked...
4
2451
by: Bruce Pullum | last post by:
I have a datagrid that I am using a DataView with. All works great for the sorting of the columns. However, after I sort the column, and then try and select a data row to edit, the row selected represents the indes of the actual DataGrid and not the DataView. For example.. Lets say I have 4 rows of data.. In the 4 rows I have an Appt Num of 1,2,3,4... Each representing a data row... I sort DESC so the rows display 4,3,2,1... If I...
2
9934
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell (multi-select without modifier keys). I got that working fine, but I also wanted to keep rows selected after a sort, which I do by storing the row's id in an arraylist. The idea was to do the sort and then go back and re-select the rows with that...
1
3064
by: BuzzLight | last post by:
Iam very new to .net, in vb6 there was the flexigrid component, you could add rows, columns .. change their width, height, header captions etc very easily... but in .net all I see is one DataGrid. I want to add columns and rows to it, change their header captions etc but it does not give me any options to add columns or rows! I have found one way of adding columns :- Dim table as new DataTable table.columns.add("C1")...
9
3384
by: John Hernry | last post by:
I have been watching the dotnet newsgroups for a couple of weeks now and scouring the net looking for a solution to my datagrid find functionality. With no luck. I am hoping that you can help me. I am an old VB6 coder used to using Sheridan controls, and I am just starting to learn the .net world. I am upgrading my music catalogue database, where the data is stored in an Access mdb file.
0
619
by: thomasp | last post by:
I have a datagridview that allows the user to sort the column by clicking on the header of the column the user wishes to sort. I did not code this, it had the ability when the datagrid was populated. My question is how can I chande the cursor to an hourglass when the user clicks the header and change it back to the default once the sort is accomplished. The database is large and the sort takes a few seconds. Need something to show the...
3
8769
by: Hugh Janus | last post by:
Hi, I have a datagrid that is not showing a vertical scrollbar even though it has more rows than displayed. I cannot find the property to set this, so after googling, i found this link: http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q876q My problem is that I don't know how to use it! Could someone please explain how to implement this in my code? The simpler the better!!
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10570
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7001
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5849
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.