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