Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / October 2005

Tip: Looking for answers? Try searching our database.

Datagrid - end edit?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John J. Hughes II - 26 Oct 2005 17:17 GMT
I have a derived data grid which shows a custom control.  This works fine.
When the something changes it calls ColumnStartedEditing which shows the
pencil.  This works fine.  If the user moves to the next row or column the
pencil leaves and the data is saved.  This works fine.

I have been told to add an "OK" and "Cancel" button to the control.  If the
user press "OK" the data is saved and if they press "Cancel" the data is not
saved.  This is very ugly but sort works if the user moves to the next row
or column.

Now for the problem.  If the user opens the data grid an edits the cell and
presses the "OK" button the data is saved correctly but the pencil is not
removed until they leave the cell.  I have tried calling commit directly,
basically save the data source and row when edit is called and then when the
"OK" button is press calling commit.  The commit is firing but not removing
the pencil and as far as I can tell not setting the record to edited.

Does anyone know how to tell the datagrid the cell is finished editing?

Regards,
John

CODE:

[Editor(typeof(DataGridTextBoxColumn), typeof(UITypeEditor))]
public class ExDataGridPhoneMenus : DataGridColumnStyle
{
private MyCtrl pmi = new MyCtrl
private Panel p = new Panel();

public ExDataGridPhoneMenus() : base()
{
 this.pmi.Visible = false;
 this.pmi.Bounds = Rectangle.Empty;
 this.p.Size = new Size(1,1);
 this.p.Visible = false;
}

protected override void ReleaseHostedControl()
{
 if(pmi != null)
 {
  pmi.Dispose();
  pmi = null;
 }
 base.ReleaseHostedControl ();
}

protected override void Abort(int rowNum)
{
 this._isEditing = false;
 this._isSave = false;
 this.pmi.SelectedItemChanged -= new EventHandler(SelectedValueChanged);
 this.pmi.WindowClosed -= new EventHandler(pmi_WindowClosed);
 this.pmi.SaveData -= new EventHandler(pmi_SaveData);
 Invalidate();
}

protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
 try
 {
  this.pmi.SelectedItemChanged -=new EventHandler(SelectedValueChanged);
  this.pmi.WindowClosed -= new EventHandler(pmi_WindowClosed);
  this.pmi.SaveData -= new EventHandler(pmi_SaveData);
  this.pmi.Visible = false;
  this.pmi.Bounds = Rectangle.Empty;

  if(this._isEditing && this._isSave)
  {
   SetColumnValueAtRow(dataSource, rowNum, this.pmi.GetData());
   Console.WriteLine("ExDataGridPhoneMenu - Committed");
  }

  this._isEditing = false;
  this._isSave = false;
 }
 catch(Exception ex)
 {
  this.Abort(rowNum);
  return false;
 }

 return true;
}

protected override void Edit(CurrencyManager source, int rowNum, Rectangle
bounds, bool readOnly, string instantText, bool cellIsVisible)
{
 try
 {
  if(this._isEditing) return;

  object idxObject = base.GetColumnValueAtRow(source, rowNum);

  if(cellIsVisible)
  {
   this.p.Bounds = new Rectangle(bounds.X, bounds.Y+bounds.Height, 1, 1);

   MenuStructure ms = (idxObject == DBNull.Value || idxObject == null)?new
MenuStructure():(MenuStructure)idxObject;

   this.pmi.il = this.displayIcons;
   this.pmi.SetValues(this.GetStrings(), this.GetIcons(),
this.GetActions(), ms.Action, ms.Type);
   this.pmi.Show();
   this.pmi.Location = this.p.PointToScreen(new Point(0,0));
   this.pmi.Size = new Size(192, 152);
   this.pmi.SelectedItemChanged +=new EventHandler(SelectedValueChanged);
   this.pmi.WindowClosed +=new EventHandler(pmi_WindowClosed);
   this.pmi.SaveData += new EventHandler(pmi_SaveData);
   this.pmi.dataSource = source;
   this.pmi.rowNum = rowNum;
  }
  else
  {
   this.pmi.Visible = false;
  }

  if(this.pmi.Visible)
   DataGridTableStyle.DataGrid.Invalidate(bounds);

 }
 catch (Exception ex)
 {
  Abort(rowNum);
 }
}

private void SelectedValueChanged(object sender, EventArgs e)
{
 this._isEditing = true;
 base.ColumnStartedEditing(this.pmi);
}

protected override void SetDataGridInColumn(DataGrid value)
{
 base.SetDataGridInColumn (value);
 if(this.p.Parent != null)
  this.p.Parent.Controls.Remove(p);
 if(value != null)
  value.Controls.Add(p);
}

protected override void ConcedeFocus()
{
 base.ConcedeFocus ();
 this.pmi.Visible = false;
}

private void pmi_WindowClosed(object sender, EventArgs e)
{
 this._isEditing = false;
 this._isSave = false;
 this.Invalidate();
}

private void pmi_SaveData(object sender, EventArgs e)
{
 this._isSave = true;
 this.Commit(this.pmi.dataSource, this.pmi.rowNum);
 this.Invalidate();
}

override protected void Paint(Graphics g, Rectangle r, CurrencyManager
source, int rowNum, Brush bakColor, Brush forColor, bool f1)
{
 /// paint screen omitted
}

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 int GetMinimumHeight() { return (13 * 2) + 13 + 5; }
protected override int GetPreferredHeight(Graphics g, object value) {
return (13 * 2) + 13 + 5; }
protected override Size GetPreferredSize(Graphics g, object value) { return
new Size (100, (13 * 2) + 13 + 5); }

private string GetAction(byte action)
{
 return "Omited";
}

private System.Data.DataView GetActions()
{
 System.Data.DataView dv = new System.Data.DataView();
 /// fill in data view
 return dv;
}

private System.Data.DataView GetIcons()
{
 System.Data.DataView dv = new System.Data.DataView();
 /// /Fill in data view
 return dv;
}

}
Bart Mermuys - 26 Oct 2005 22:39 GMT
Hi,

>I have a derived data grid which shows a custom control.  This works fine.
>When the something changes it calls ColumnStartedEditing which shows the
[quoted text clipped - 15 lines]
>
> Does anyone know how to tell the datagrid the cell is finished editing?

When the user presses ok then call:
 DataGridTableStyle.DataGrid.EndEdit( this, -1, false );
 DataGridTableStyle.DataGrid.Invalidate(true);
 // here EndEdit will invoke Commit, so you don't need to call it

When the user presses cancel then call:
 DataGridTableStyle.DataGrid.EndEdit( this, -1, true );
 // here EndEdit will invoke Abort

I tried to put this code into your code, but not everything was clear, like
the panel, so i removed it, your usercontrol should also have two events
CancelClick and OkClick, these events should be fired from the two buttons.

 [Editor(typeof(DataGridTextBoxColumn), typeof(UITypeEditor))]
 public class ExDataGridPhoneMenus : DataGridColumnStyle
 {
   private MyCtrl pmi = new MyCtrl();
   private bool _isEditing;

   public ExDataGridPhoneMenus() : base()
   {
     pmi.Visible = false;
     pmi.Bounds = Rectangle.Empty;
     pmi.OkClick +=new EventHandler(pmi_OkClick);
     pmi.CancelClick += new EventHandler(pmi_CancelClick);
   }

   protected override void SetDataGridInColumn(DataGrid value)
   {
     base.SetDataGridInColumn (value);

     if(pmi.Parent != null)
       pmi.Parent.Controls.Remove(pmi);

     if(value != null)
     {
       value.Controls.Add(pmi);
     }
   }

   protected override void ReleaseHostedControl()
   {
     if(pmi.Parent != null)
     {
       pmi.Parent.Controls.Remove(pmi);
     }
   }

   protected override void Edit(CurrencyManager source, int rowNum,
Rectangle
     bounds, bool readOnly, string instantText, bool cellIsVisible)
   {
     try
     {
       if(cellIsVisible && !_isEditing)
       {
         object idxObject = base.GetColumnValueAtRow(source, rowNum);

         MenuStructure ms = (idxObject == DBNull.Value || idxObject ==
null)?new MenuStructure():(MenuStructure)idxObject;
         pmi.il = displayIcons;
         pmi.SetValues(GetStrings(), GetIcons(), GetActions(), ms.Action,
ms.Type);
         pmi.dataSource = source;
         pmi.rowNum = rowNum;

         pmi.Show();
         pmi.Bounds = bounds;

         pmi.SelectedItemChanged+=
           new EventHandler(pmi_SelectedValueChanged);

         Invalidate();
       }
     }
     catch (Exception ex)
     {
       Console.WriteLine("Edit {0}", ex);
     }
   }

   private void HideUserControl()
   {
     pmi.SelectedItemChanged-=
       new EventHandler(pmi_SelectedValueChanged);

     pmi.Hide();
     Invalidate();
   }

   protected override void Abort(int rowNum)
   {
     Console.WriteLine("Abort Called");
     if(_isEditing)
     {
       HideUserControl();
       _isEditing = false;
     }
   }

   protected override bool Commit(CurrencyManager dataSource, int rowNum)
   {
     try
     {
       if(_isEditing)
       {
         Console.WriteLine("ExDataGridPhoneMenu - Committed");
         SetColumnValueAtRow( dataSource, rowNum, pmi.GetData );

         HideUserControl();
         _isEditing = false;
       }
     }
     catch(Exception ex)
     {
       Console.WriteLine("ExDataGridPhoneMenu - Committed {0}", ex);
       return false;
     }

     return true;
   }

   private void pmi_SelectedValueChanged(object sender, EventArgs e)
   {
     _isEditing = true;
     base.ColumnStartedEditing(pmi);
   }

   private void pmi_OkClick(object sender, EventArgs e)
   {
     if ( _isEditing )
     {
       DataGridTableStyle.DataGrid.EndEdit( this, -1, false );
       DataGridTableStyle.DataGrid.Invalidate(true);
     }
     else
        HideUserControl();
   }

   private void pmi_CancelClick(object sender, EventArgs e)
   {
     if ( _isEditing )
        DataGridTableStyle.DataGrid.EndEdit( this, -1, true );
     else
        HideUserControl();
   }

   /* other functions omitted */
}

HTH,
Greetings
John J. Hughes II - 27 Oct 2005 15:00 GMT
Bart,

Thank you very much for the help!

Your suggestion and fixing another error in my code fixed the problem 99%
now I just have one minor problem which I guess I can ignore but if you have
a suggestion I would appreciate it.

The Abort and Commit are being called correctly when the windows closes,
cancel button is press, or the ok button is pressed and the data is either
saved or not saved as expected which is great.

The pencil disappears when the commit it called but it does not disappear
when the abort is call.  Do you have any idea how I can get the pencil to
disappear when the abort is call?

Below is the sequence being called:
ExDataGridPhoneMenu - SelectedValueChanged
ExDataGridPhoneMenu - pmi_OnCancel
ExDataGridPhoneMenu - Abort

I would think "DataGridTableStyle.DataGrid.EndEdit( this, -1, true );" would
clear the pencil since the abort function really does not affect the
datagrid directly.

The panel is the control hosted by the cell and is where I draw a
representation of the data.  PMI is the edit control to edit the data but is
not displayed unless in edit mode.  Since the data is graphical in nature it
can not be edited from the keypad or the cell (not big enough) the PMI
control is shown to allow for more editing room.

Regards,
John
Bart Mermuys - 27 Oct 2005 15:34 GMT
Hi,

> Bart,
>
[quoted text clipped - 11 lines]
> when the abort is call.  Do you have any idea how I can get the pencil to
> disappear when the abort is call?

Doesn't seem to look like a big problem, add a call to Invalidate in the
CancelClick handler (similar to the OkClick handler):

private void pmi_CancelClick(object sender, EventArgs e)
{
  if ( _isEditing )
 {
    DataGridTableStyle.DataGrid.EndEdit( this, -1, true);
    DataGridTableStyle.DataGrid.Invalidate(true);
  }
  else
 {
    HideUserControl();
  }
}

HTH,
Greetings

> Below is the sequence being called:
> ExDataGridPhoneMenu - SelectedValueChanged
[quoted text clipped - 13 lines]
> Regards,
> John
John J. Hughes II - 27 Oct 2005 16:28 GMT
Bart,

That did it, great! thanks again.

I had tried invalidating but not on that level.

Regards,
John

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.