DataGrid Q: How to force a column to go muti line if necessary to handle the text

  • Thread starter Thread starter Glorfindel
  • Start date Start date
G

Glorfindel

I want to have all my columns visible in the window. I've found out how to
disable the column resizing, and even to force the horizontal scroll bar to
hide itself. What I dont know (if it can be done in the WinForms control)
is how to force a cell to go multi line if necessary to show text. I've
seen it done in the Web DataGrid.

For the edification of everyone, here's how to disable column sizing, and
hide the horizontal scroll bar. This, when combined with proper sizing of
the columns, puts everthing on the screen for you:

Do do any of this you must derive your own DataGrid and override stuff.
To make the columns fixed (to prevent the user from increasing their size
and necessitating a scroll bar) you must override OnMouseMove and
OnMouseDown and stop it from calling the base class when a column resize is
being done. To force the scroll bar to hide, hook into the VisibleChanged
event and force it to hide there.

public class MyDataGrid : System.Windows.Forms.DataGrid

{

public MyDataGrid()

{

this.HorizScrollBar.Visible = false;

this.HorizScrollBar.VisibleChanged += new
EventHandler(HorizScrollBar_VisibleChanged);

}


protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)

{

DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));

if (hti.Type == DataGrid.HitTestType.ColumnResize)

{

return; //omit base class call

}

base.OnMouseMove (e);

}


protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)

{

DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));

if (hti.Type == DataGrid.HitTestType.ColumnResize)

{

return; //omit base class call

}

base.OnMouseDown (e);

}

private void HorizScrollBar_VisibleChanged(object sender, EventArgs e)

{

if(this.HorizScrollBar.Visible == true)

{

this.HorizScrollBar.Hide();

}

}

}
 
Back
Top