ComboBox dans un DataGrid --> problème à la validation

Résolu
cfeltz Messages postés 8 Date d'inscription mercredi 5 juillet 2006 Statut Membre Dernière intervention 13 juillet 2006 - 11 juil. 2006 à 17:12
cfeltz Messages postés 8 Date d'inscription mercredi 5 juillet 2006 Statut Membre Dernière intervention 13 juillet 2006 - 13 juil. 2006 à 10:22
Bonjour,
je voudrais mettre un combo dans un DataGrid, après avoir fait quelque recherche sur le net j'ai réussi à trouver plus ou moins comment faire (voir le source qui suis).

mais je m'y suis sans doute mal pris...

Mon combo apparaît bien avec les bonnes valeurs, mais pour une raison que j'ignore lorsque je sélectionne une valeur dans le combo et que je clic ailleur, la cellule du DataGrid reprend l'ancienne valeur.
Rq.: si je modifie la valeur à la main (sans faire de sélection dans le combo), ça marche comme il faut.

Si quelqu'un peut m'expliquer ce que j'ai mal fait... merci d'avance

Annexe :

using

System
;
using System
.Data
;
using System
.Drawing
;
using System
.Windows
.Forms
;
namespace DataGridComponants
{
   public class DataGridComboBox : DataGridColumnStyle

      private ComboBox _ComboGrid
= null
;
      private bool isEditing

      public ComboBox ComboBox
{
         get {return _ComboGrid
;}
         set {_ComboGrid
= value
;}
      }
      public DataGridComboBox() : base
() { 
         _ComboGrid
= new ComboBox (); 
         _ComboGrid
.Visible
= false
      } 
      protected override void Abort(int rowNum
) { 
         isEditing
= false ; 
         _ComboGrid
.TextChanged
- = new EventHandler(_ComboGridValueChanged
); 
         Invalidate(); 
      } 
      protected override bool Commit(CurrencyManager dataSource
, int rowNum
) { 
         _ComboGrid
.Bounds
= Rectangle
.Empty

         _ComboGrid
.TextChanged
-= new EventHandler (_ComboGridValueChanged
); 
         if (!isEditing
) return true
         isEditing
= false
         try
            string value
= _ComboGrid
.Text

            SetColumnValueAtRow (dataSource
, rowNum
, value
); 
         } 
         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
) { 
         string value
= GetColumnValueAtRow(source
, rowNum
).ToString(); 
         if (cellIsVisible
) { 
            _ComboGrid
.Bounds
= new Rectangle (bounds
.X
, bounds
.Y
, bounds
.Width
, bounds
.Height
); 
            _ComboGrid
.Text
= value

            _ComboGrid
.Visible
= true ; 
            _ComboGrid
.TextChanged
+ = new EventHandler(_ComboGridValueChanged
); 
         } else
            _ComboGrid
.Text
= value

            _ComboGrid
.Visible
= false
         } 
         if (_ComboGrid
.Visible
) DataGridTableStyle
.DataGrid
.Invalidate(bounds
); 
      } 
      protected override Size
GetPreferredSize(Graphics g
, object value
) { 
         return new Size(100, _ComboGrid
.PreferredHeight
); 
      } 
      protected override int GetMinimumHeight() { 
         return _ComboGrid
.PreferredHeight

      } 
      protected override int GetPreferredHeight(Graphics g
, object value
) { 
         return _ComboGrid
.PreferredHeight

      } 
      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 str
= GetColumnValueAtRow (source
, rowNum
).ToString(); 
         Rectangle rect
= bounds

         g
.FillRectangle(backBrush
,rect
); 
         rect
.Offset(0, 2); 
         rect
.Height
-= 2; 
         g
. DrawString (str
, this
.DataGridTableStyle
.DataGrid
.Font
, foreBrush
, rect
); 
      } 
      protected override void SetDataGridInColumn(DataGrid value
) { 
         base
.SetDataGridInColumn(value
); 
         if (_ComboGrid
.Parent
! = null
) { 
            _ComboGrid
.Parent
.Controls
.Remove (_ComboGrid
); 
         } 
         if (value
!= null
) { 
            value
.Controls
.Add(_ComboGrid
); 
         } 
      } 
      private void _ComboGridValueChanged(object sender
, EventArgs e
) { 
         this
.isEditing
= true
         base
.ColumnStartedEditing(_ComboGrid
); 
      } 
   }
}

1 réponse

cfeltz Messages postés 8 Date d'inscription mercredi 5 juillet 2006 Statut Membre Dernière intervention 13 juillet 2006
13 juil. 2006 à 10:22
Pour ce que ça interresse le problème du source qui précède se trouve dans la fonction Edit
la ligne
_ComboGrid.TextChanged += new EventHandler
(_ComboGridValueChanged); 
doit se trouver avant
_ComboGrid
.Text = value


Voici également deux liens traitant du sujet :
http://www.codeproject.com/cs/miscctrl/datagridcomboboxcolumn.asp
et
http://msdn2.microsoft.com/fr-fr/library/system.windows.forms.datagridcolumnstyle.propertydescriptor.aspx

Cordialement++
3
Rejoignez-nous