Overlapping scrollbar arrows in datagrid?

G

Guest

Hi,

I have a problem with a datagrid. On its first display, the scrollbars
arrows look fine. However when I display it another time (after returning
from another form), the horizontal scrollbar's right arrow is overlapping the
vertical bar's down arrow. Just wondering if anyone has experienced a
similar behavior? Suggestions?

thanks,
Michel
 
J

John Kendrick

I've encounter several graphics related issues with the DataGrid in the
compact framework. For the overlapping scrollbar issue I wrote a function
that recalculated and applied the new scrollbar sizes. Here is the function
I used:

/// <summary>

/// Fixes the DataGrid ScrollBar bug that causes the Vertical

/// ScrollBar to overlap the Horizontal ScrollBar when both are

/// visible.

/// </summary>

/// <remarks>

/// If only the Vertical or only the Horizontal scrollbar is visible

/// then the scrollbars are drawn using the default size. To minimize

/// reflection calls, conditional checks are made as soon as the data

/// is available.

/// <p>

/// This must only be called after the DataSource has been set.

/// </p>

/// </remarks>

/// <example> This example shows how to use the function
<b>FixDataGridScrollBars</b>.

/// <code>

/// [C#]

/// public void InitializeDataGrid (DataGrid dg, DataSet ds)

/// {

/// // Bind data to the grid.

/// dg.DataSource = ds;

///

/// // Specify the Table in the DataSet to disply.

/// dg.DataMember = "MyTable";

///

/// // Fix the ScrollBars' overlap problem if it exists.

/// FixDataGridScrollBars(dg);

/// }

/// </code>

/// </example>

/// <param name="dg">DataGrid object.</param>

public static void FixDataGridScrollBars(DataGrid dg)

{

// The GridRender is .NET data type that cannot be directly

// created, therefore it is returned as an object. Since there are

// no public methods or properties available to retreive the

// data more reflection calls are required on the renderer object.

object renderer = dg.GetType().GetField("m_renderer",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(dg);

int rowsHeight = Convert.ToInt32(renderer.GetType().GetField("m_cyAllRows",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(renderer));

// Bail out now if the overlap condition cannot occur

if (rowsHeight < dg.Height)

return;

// Get the width from the renderer object and Vertical ScrollBar.

int colsWidth = Convert.ToInt32(renderer.GetType().GetField("m_cxAllCols",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(renderer));

ScrollBar sbVert = (ScrollBar)dg.GetType().GetField("m_sbVert",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(dg);

sbVert.Visible = true;

// Bail out now if the overlap condition cannot occur

if (colsWidth < (dg.Width - sbVert.Width))

return;

// We know an adjust is required so get the Horizontal ScrollBar.

ScrollBar sbHorz = (ScrollBar)dg.GetType().GetField("m_sbHorz",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(dg);

sbHorz.Visible = true;

// Adjust the ScrollBar's size to prevent the overlap

sbHorz.Width = dg.Width - sbVert.Width;

sbVert.Height = dg.Height - sbHorz.Height;

}
 
G

Guest

Hi John,

That worked wonderfully, thanks a lot! I'm not sure why the code I had that
was taking care of this decided to stop working. At first I thought it was
SP3-related, so I went back to SP2 but it was still doing the same thing, so
maybe it just didn't work in all situations and I hadn't noticed before.

Thanks again,
Michel

John Kendrick said:
I've encounter several graphics related issues with the DataGrid in the
compact framework. For the overlapping scrollbar issue I wrote a function
that recalculated and applied the new scrollbar sizes. Here is the function
I used:

/// <summary>

/// Fixes the DataGrid ScrollBar bug that causes the Vertical

/// ScrollBar to overlap the Horizontal ScrollBar when both are

/// visible.

/// </summary>

/// <remarks>

/// If only the Vertical or only the Horizontal scrollbar is visible

/// then the scrollbars are drawn using the default size. To minimize

/// reflection calls, conditional checks are made as soon as the data

/// is available.

/// <p>

/// This must only be called after the DataSource has been set.

/// </p>

/// </remarks>

/// <example> This example shows how to use the function
<b>FixDataGridScrollBars</b>.

/// <code>

/// [C#]

/// public void InitializeDataGrid (DataGrid dg, DataSet ds)

/// {

/// // Bind data to the grid.

/// dg.DataSource = ds;

///

/// // Specify the Table in the DataSet to disply.

/// dg.DataMember = "MyTable";

///

/// // Fix the ScrollBars' overlap problem if it exists.

/// FixDataGridScrollBars(dg);

/// }

/// </code>

/// </example>

/// <param name="dg">DataGrid object.</param>

public static void FixDataGridScrollBars(DataGrid dg)

{

// The GridRender is .NET data type that cannot be directly

// created, therefore it is returned as an object. Since there are

// no public methods or properties available to retreive the

// data more reflection calls are required on the renderer object.

object renderer = dg.GetType().GetField("m_renderer",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(dg);

int rowsHeight = Convert.ToInt32(renderer.GetType().GetField("m_cyAllRows",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(renderer));

// Bail out now if the overlap condition cannot occur

if (rowsHeight < dg.Height)

return;

// Get the width from the renderer object and Vertical ScrollBar.

int colsWidth = Convert.ToInt32(renderer.GetType().GetField("m_cxAllCols",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(renderer));

ScrollBar sbVert = (ScrollBar)dg.GetType().GetField("m_sbVert",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(dg);

sbVert.Visible = true;

// Bail out now if the overlap condition cannot occur

if (colsWidth < (dg.Width - sbVert.Width))

return;

// We know an adjust is required so get the Horizontal ScrollBar.

ScrollBar sbHorz = (ScrollBar)dg.GetType().GetField("m_sbHorz",

BindingFlags.NonPublic|

BindingFlags.Static|

BindingFlags.Instance).GetValue(dg);

sbHorz.Visible = true;

// Adjust the ScrollBar's size to prevent the overlap

sbHorz.Width = dg.Width - sbVert.Width;

sbVert.Height = dg.Height - sbHorz.Height;

}


Michel Renaud said:
Hi,

I have a problem with a datagrid. On its first display, the scrollbars
arrows look fine. However when I display it another time (after returning
from another form), the horizontal scrollbar's right arrow is overlapping the
vertical bar's down arrow. Just wondering if anyone has experienced a
similar behavior? Suggestions?

thanks,
Michel
 
S

sunadh

Hi John,

Thanks a lot. Your code works like magic. here is a vb.net code I have converted and tested works like a wonder.

Public Sub FixDataGridScrollBars(ByVal dg As DataGrid)

' The GridRender is .NET data type that cannot be directly
' created, therefore it is returned as an object. Since there are
' no public methods or properties available to retreive the
' data more reflection calls are required on the renderer object.

Dim Renderer As Object = dg.GetType().GetField("m_renderer", BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance).GetValue(dg)
Dim rowsHeight As Integer = Convert.ToInt32(Renderer.GetType().GetField("m_cyAllRows", BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance).GetValue(Renderer))
' Bail out now if the overlap condition cannot occur
If rowsHeight < dg.Height Then
Return
End If

' Get the width from the renderer object and Vertical ScrollBar.
Dim colsWidth As Integer = Convert.ToInt32(Renderer.GetType().GetField("m_cxAllCols", BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance).GetValue(Renderer))
Dim sbVert As ScrollBar = CType(dg.GetType().GetField("m_sbVert", BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance).GetValue(dg), ScrollBar)

sbVert.Visible = True

' Bail out now if the overlap condition cannot occur
If colsWidth < (dg.Width - sbVert.Width) Then
Return
End If


Dim sbHorz As ScrollBar = CType(dg.GetType().GetField("m_sbHorz", BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance).GetValue(dg), ScrollBar)
sbHorz.Visible = True

' Adjust the ScrollBar's size to prevent the overlap
sbHorz.Width = dg.Width - sbVert.Width
sbVert.Height = dg.Height - sbHorz.Height
End Sub

From http://www.developmentnow.com/g/18_2005_2_0_0_157639/Overlapping-scrollbar-arrows-in-datagrid.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 

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