Re: Custom controls in datagrid

From: CJ (cjohns_at_telkomsa.net)
Date: 06/03/04


Date: Thu, 3 Jun 2004 12:19:50 +0200

In article <#K#3GcISEHA.2936@TK2MSFTNGP12.phx.gbl>, ernest@gesora.com
says...
> I have made some changes :

Thanks, that helps a lot :-)

But there's still one issue: if I type in the right-hand column new row,
and then click in the left-hand column of the same row, commit is not
called. If I do this in reverse, ie. left column then right column, it
works perfectly.

Is there some intrinsic ordering to the columns?

Interestingly, if I make one column my custom columnstyle, but leave the
other as the standard TextBoxColumn, it all works fine.

> 1. I changed the RichTextDragBox in RichTextBox to have the possibility to
> make tests.
>
> 2. In the Edit method ,at the begining I added:
> if(this.isEditing )
> {
> this.rtdb.Focus();
> return ;
> }
> When you type the first character on a new row, the framework calls the Edit
> for the second time(when the isEditing is already true) , without calling
> Commit first.
> 3. In the Edit method :
> rtdb.Bounds=bounds;
> to make the rtdb to occupie all the surface of the cell.
> 4. In the Edit method :
> I changed the position of the :
> rtdb.TextChanged += new EventHandler(RichTextBoxChanged);
> to the end of the method.
> In the old version you associated the event handler and then rtdb.Text =
> val. That made to fire the event RichTextBoxChanged and the row apeared as
> "modified" when it got the focus and , in fact, it isn't modified.
>
> 5. in the event handler RichTextBoxChanged I added:
> rtdb.TextChanged -= new EventHandler(RichTextBoxChanged)
> The code in this event handler needs to executed just once at any new
> editing session. In the old version, you executed this code at any character
> pressed on the keyboard.
>
> This is the new version of your class :
> ////////////////////////////////////////////////////////////////////////////
> /////////////////////////////////////////////
> using System.Windows.Forms ;
> using System.Drawing ;
> using System;
> using System.Diagnostics ;
> namespace ExtendedDataGrid.ColumnStyles
> {
> public class DataGridRichTextBoxColumn : DataGridTextBoxColumn
> {
> public RichTextBox rtdb = new RichTextBox();
> // The isEditing field tracks whether or not the user is
> // editing data with the hosted control.
> private bool isEditing;
>
> public DataGridRichTextBoxColumn() : base()
> {
> rtdb.Visible = false;
> this.NullText = "";
> }
> protected override void Abort(int rowNum)
> {
> isEditing = false;
> rtdb.TextChanged -= new EventHandler
> (RichTextBoxChanged);
> Invalidate();
> }
> protected override bool Commit(CurrencyManager dataSource,
> int rowNum)
> {
> Console.WriteLine("Commit");
> rtdb.Bounds = Rectangle.Empty;
> rtdb.TextChanged -= new EventHandler
> (RichTextBoxChanged);
> if (!isEditing)
> return true;
> isEditing = false;
> try
> {
> string val = rtdb.Text;
> SetColumnValueAtRow(dataSource, rowNum, val);
> }
> catch (Exception)
> {
> Abort(rowNum);
> return false;
> }
> Invalidate();
> return true;
> }
> protected override void Edit(CurrencyManager source, int
> rowNum, Rectangle bounds,
> bool readOnly, string instantText, bool
> cellIsVisible)
> {
> if(this.isEditing )
> {
> this.rtdb.Focus();
> return ;
> }
> string val;
> object o = GetColumnValueAtRow(source, rowNum);
> if (Convert.IsDBNull(o))
> val = this.NullText;
> else
> val = (string)o;
> Console.WriteLine("IT: _" + instantText + "_ val: "
> + val);
> if (cellIsVisible)
> {
> // rtdb.Bounds = new Rectangle
> // (bounds.X + 2, bounds.Y + 2,
> // bounds.Width - 4, bounds.Height - 4);
> rtdb.Bounds=bounds;
> rtdb.Text = val;
> rtdb.Visible = true;
> rtdb.BringToFront();
> rtdb.Focus();
> }
> else
> {
> rtdb.Text = val;
> rtdb.Visible = false;
> }
> if (rtdb.Visible)
> DataGridTableStyle.DataGrid.Invalidate
> (bounds);
> rtdb.TextChanged += new EventHandler
> (RichTextBoxChanged);
> }
> private void RichTextBoxChanged(object sender,
> System.EventArgs e)
> {
> Console.WriteLine("DragBoxChanged");
> this.isEditing = true;
> base.ColumnStartedEditing(rtdb);
> rtdb.TextChanged -= new EventHandler
> (RichTextBoxChanged);
> }
> protected override Size GetPreferredSize(Graphics g, object
> val)
> {
> return new Size(100, rtdb.PreferredHeight + 4);
> }
> protected override int GetMinimumHeight()
> {
> return rtdb.PreferredHeight + 4;
> }
> protected override int GetPreferredHeight(Graphics g,
> object val)
> {
> return rtdb.PreferredHeight + 4;
> }
> protected override void Paint(Graphics g, Rectangle bounds,
> CurrencyManager source, int rowNum)
> {
> Paint(g, bounds, source, rowNum, false);
> }
> protected override void Paint(Graphics g, Rectangle bounds,
> CurrencyManager source,
> int rowNum, bool alignToRight)
> {
> Paint(g,bounds, source, rowNum, Brushes.Red,
> Brushes.Blue, alignToRight);
> }
> protected override void Paint(Graphics g, Rectangle bounds,
> CurrencyManager source,
> int rowNum, Brush backBrush, Brush foreBrush, bool
> alignToRight)
> {
> string val;
> object o = GetColumnValueAtRow(source, rowNum);
> if (Convert.IsDBNull(o))
> val = this.NullText;
> else
> val = (string)o;
> Rectangle rect = bounds;
> g.FillRectangle(backBrush,rect);
> rect.Offset(0, 2);
> rect.Height -= 2;
> g.DrawString(val,
> this.DataGridTableStyle.DataGrid.Font, foreBrush, rect);
> }
> protected override void SetDataGridInColumn(DataGrid val)
> {
> base.SetDataGridInColumn(val);
> if (rtdb.Parent != null)
> {
> rtdb.Parent.Controls.Remove(rtdb);
> }
> if (val != null)
> {
> val.Controls.Add(rtdb);
> }
> }
> }
> }
> ////////////////////////////////////////////////////////////////////////////
> ///////////////////////////////////////////////////////////////
> Ernest
>
>
>

-- 
CJ


Relevant Pages

  • Problems with DataGrid
    ... private DataGridControlColumn dataGridTextBoxColumn2; ... protected override void Dispose(bool disposing) ... int rowNum) ...
    (microsoft.public.dotnet.languages.csharp)
  • Problems with datagrid
    ... private DataGridControlColumn dataGridTextBoxColumn2; ... protected override void Dispose(bool disposing) ... int rowNum) ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Using a RichTextBox instead of TextBox in Datagrid
    ... > protected override void Abort(int rowNum) ... > protected override bool Commit(CurrencyManager dataSource, ... > protected override void Edit(CurrencyManager source, ... > CurrencyManager source, int rowNum) ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Custom controls in datagrid
    ... >> protected override bool Commit(CurrencyManager dataSource, ... >> protected override void Edit(CurrencyManager source, ... >> CurrencyManager source, int rowNum) ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Why my NumericUpDown DataGrid Column cant set the value for n
    ... public class DataGridNumericUpDownColumn: DataGridColumnStyle ... protected override void Abort(int rowNum) ... protected override bool Commit(CurrencyManager dataSource, ...
    (microsoft.public.dotnet.framework.windowsforms)

Loading