2008-12-08 Ivan N. Zlatev <contact@i-nz.net>
authorIvan Zlatev <ivan@ivanz.com>
Mon, 8 Dec 2008 02:21:51 +0000 (02:21 -0000)
committerIvan Zlatev <ivan@ivanz.com>
Mon, 8 Dec 2008 02:21:51 +0000 (02:21 -0000)
* DataGridViewComboBoxCell.cs: Implement data binding support.

svn path=/trunk/mcs/; revision=120969

mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewComboBoxCell.cs

index 4ad5bb15bdbd4e11082dc285a36601ccb6cf841a..19016f0929cf2f9e6360daddf7c1271c3807c353 100644 (file)
@@ -1,3 +1,7 @@
+2008-12-08  Ivan N. Zlatev  <contact@i-nz.net>
+
+       * DataGridViewComboBoxCell.cs: Implement data binding support.
+
 2008-12-08  Ivan N. Zlatev  <contact@i-nz.net>
 
        * DataGridView.cs: Use the CurrencyManager to update the data source 
index 1d258b38932b7e05295049abb0d89c8d6f4bc4de..c1be363ae83da408aace7a54009721459848cdec 100644 (file)
@@ -21,6 +21,7 @@
 //
 // Author:
 //     Pedro Martínez Juliá <pedromj@gmail.com>
+//     Ivan N. Zlatev <contact@i-nz.net>
 //
 
 
@@ -46,8 +47,6 @@ namespace System.Windows.Forms {
                private bool sorted;
                private string valueMember;
 
-               private DataGridViewComboBoxEditingControl editingControl;
-
                public DataGridViewComboBoxCell () : base() {
                        autoComplete = true;
                        dataSource = null;
@@ -127,7 +126,20 @@ namespace System.Windows.Forms {
 
                [Browsable (false)]
                public virtual ObjectCollection Items {
-                       get { return items; }
+                       get {
+                               items.Clear ();
+
+                               if (DataGridView != null && DataGridView.BindingContext != null && 
+                                   DataGridView.DataSource != null) {
+                                       CurrencyManager manager = DataGridView.BindingContext[DataGridView.DataSource] as CurrencyManager;
+                                       if (manager != null) {
+                                               items.AddRange (manager.List);
+                                       }
+                                       
+                               }
+
+                               return items;
+                       }
                }
 
                [DefaultValue (8)]
@@ -169,6 +181,8 @@ namespace System.Windows.Forms {
                        cell.autoComplete = this.autoComplete;
                        cell.dataSource = this.dataSource;
                        cell.displayStyle = this.displayStyle;
+                       cell.displayMember = this.displayMember;
+                       cell.valueMember = this.valueMember;
                        cell.displayStyleForCurrentCellOnly = this.displayStyleForCurrentCellOnly;
                        cell.dropDownWidth = this.dropDownWidth;
                        cell.flatStyle = this.flatStyle;
@@ -185,32 +199,23 @@ namespace System.Windows.Forms {
                public override void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) {
                        base.InitializeEditingControl (rowIndex, initialFormattedValue, dataGridViewCellStyle);
                        
-                       editingControl = DataGridView.EditingControl as DataGridViewComboBoxEditingControl;
-                       
-                       if (editingControl == null)
-                               return;
-                       
-                       string text = initialFormattedValue == null ? string.Empty : initialFormattedValue.ToString ();
-
-                       // A simple way to check if the control has
-                       // been initialized already.
-                       if (editingControl.Items.Count > 0) {
-                               editingControl.SelectedIndex = editingControl.FindString (text);
-                               return;
-                       }
+                       ComboBox editingControl = DataGridView.EditingControl as ComboBox;
                        
                        editingControl.DropDownStyle = ComboBoxStyle.DropDownList;
-                       editingControl.Items.AddRange (this.Items);
-                       
-                       editingControl.Sorted = sorted;
-                       editingControl.SelectedIndex = editingControl.FindString (text);
-
-                       editingControl.SelectedIndexChanged += new EventHandler (editingControl_SelectedIndexChanged);
-               }
-
-               void editingControl_SelectedIndexChanged (object sender, EventArgs e)
-               {
-                       Value = editingControl.SelectedItem;
+                       editingControl.Sorted = Sorted;
+                       editingControl.DataSource = null;
+                       editingControl.ValueMember = null;
+                       editingControl.DisplayMember = null;
+                       editingControl.Items.Clear();
+                       editingControl.SelectedIndex = -1;
+
+                       if (DataGridView.DataSource != null) {
+                               editingControl.DataSource = DataGridView.DataSource;
+                               editingControl.ValueMember = ValueMember;
+                               editingControl.DisplayMember = DisplayMember;
+                       } else {
+                               editingControl.Items.AddRange (this.Items);
+                       }
                }
 
                public override bool KeyEntersEditMode (KeyEventArgs e)