Click here to Skip to main content
Licence CPOL
First Posted 4 Jun 2004
Views 315,836
Bookmarked 80 times

ComboBox in a DataGrid

By | 13 Sep 2006 | Article
How to embed a ComboBox (DropDownList) in a DataGrid.

Introduction

I needed a ComboBox in my DataGrid. After looking around on the web, I found many examples, but none of them worked for me.

With inspiration from Alastair Stells' article here on The Code Project and whatever else I found on the Internet, I have made the following DataGridComboBoxColumn class.

Why did the other examples not work

All the other examples populate the ComboBox with a DataView, but I need to (want to be able to) populate my ComboBox with an IList (ArrayList) instead of a DataView.

columnComboBox = new DataGridComboBoxColumn();
columnComboBox.comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
columnComboBox.comboBox.DisplayMember = "Name";
columnComboBox.comboBox.ValueMember = "GUID";

And MyDataClass.GetArray() returns MyDataClass[], and has two properties named Name and GUID.

The other examples expect columnComboBox.comboBox.DataSource to be a DataView, and it being an ArrayList generates exceptions.

I use the ComboBox to fetch display text

Since you don't know the type of columnComboBox.comboBox.DataSource, you can't use that to translate between the underlying data and what to display in the DataGrid.

Instead, I use the ComboBox itself, by overriding the ComboBox and implementing this method.

public string GetDisplayText(object value) {
   // Get the text.
   string text   = string.Empty;
   int  memIndex  = -1;
   try {
      base.BeginUpdate();
      memIndex     = base.SelectedIndex;
      base.SelectedValue = value.ToString();
      text      = base.SelectedItem.ToString();
      base.SelectedIndex = memIndex;
   } catch {
     return GetValueText(0);
   } finally {
      base.EndUpdate();
   }

   return text;
} // GetDisplayText

What I do is simple. I select the item which displays the text I want, get the text, and then reselect the original item. By doing it this way, it doesn't matter what data source is used.

Because I use the ComboBox itself to fetch the display text, the ComboBox must be populated before the DataGrid is drawn.

Alastair Stells noted about this in his article:

Another issue which arose was an eye-opener! I discovered the ComboBox does not get populated until the ComboBox.Visible property is set for the first time.

This means that the ComboBox can't be used to fetch the initial display text, because it is not visible when the DataGrid is first shown (painted).

I used a normal ComboBox to illustrate the problem and the solution.

ComboBox comboBox = new ComboBox();
comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
comboBox.DisplayMember = "Name"
comboBox.ValueMember = "GUID"
MessageBox.Show(comboBox.Items.Count.ToString()); // THIS IS ALWAYS 0!

I learned that it didn't help to show the ComboBox, but instead I had to set its parent - which internally commits the data from the DataSource to the Items collection.

ComboBox comboBox = new ComboBox();
comboBox.Parent = this; // this is a Form instance in my case.
comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
comboBox.DisplayMember = "Name"
comboBox.ValueMember = "GUID"
// THIS IS MyDataClass.GetArray().Count
MessageBox.Show(comboBox.Items.Count.ToString());

What else about my DataGridComboBoxColumn

The source code is straightforward. First, I inherited DataGridTextBoxColumn, but my class then evolved into inheriting DataGridColumnStyle. This meant that I had to implement the Paint methods, but at this point, I had some examples of that as well. I like the idea of not having an invisible TextBox behind it all.

How to use

Sadly, I don't know how to "register" my DataGridComboBoxColumn with the GridColumnStyles, enabling me to design the DataGrid columns in the designer. This code does it manually:

// Add three MyDataClass objects, to the DataGridComboBox.
// This is the choices which will apear in the ComboBox in the DataGrid.
// You can see in the source that the MyDataClass doubles
// as a static collection, where the new MyDataClass objects
// automatically is added.
// All the MyDataClass objects can be retreived in an array
// with the static method: MyDataClass.GetArray().
if (MyDataClass.GetArray().Length == 0) {
    new MyDataClass("Denmark");
    new MyDataClass("Faroe Islands (DK)");
    new MyDataClass("Finland");
    new MyDataClass("Greenland (DK)");
    new MyDataClass("Iceland");
    new MyDataClass("Norway");
    new MyDataClass("Sweden");
}


// I don't have a database here, so I make my
// own DataTable with two columns and finally
// populate it with some test rows.
DataTable table = new DataTable("TableOne");

DataColumn column = table.Columns.Add();
column.ColumnName = "country";
// Realy a GUID from the DataGridComboBox.
column.DataType = Type.GetType("System.Guid");

column = table.Columns.Add();
column.ColumnName = "notes";
column.DataType = Type.GetType("System.String");

table.Rows.Add(new object[] {MyDataClass.GetArray()[0].GUID, 
                             "Population 5.368.854"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[1].GUID, 
                             "Population 46.011"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[2].GUID, 
                             "Population 5.183.545"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[3].GUID, 
                             "Population 56.376"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[4].GUID, 
                             "Population 279.384"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[5].GUID, 
                             "Population 4.525.116"});
table.Rows.Add(new object[] {MyDataClass.GetArray()[6].GUID, 
                             "Population 8.876.744"});

// Create a DataGridTableStyle object.
DataGridTableStyle tableStyle = new DataGridTableStyle();
DataGridTextBoxColumn columnTextBox;
DataGridComboBoxColumn columnComboBox;
tableStyle.RowHeadersVisible = true;
tableStyle.RowHeaderWidth = 20;

// Add customized columns.
// Column "notes", which is a simple text box.
columnTextBox = new DataGridTextBoxColumn();
columnTextBox.MappingName = "notes";
columnTextBox.HeaderText = "Country notes";
columnTextBox.Width = 200;
tableStyle.GridColumnStyles.Add(columnTextBox);

// Column "country", which is the ComboBox.
columnComboBox = new DataGridComboBoxColumn();
columnComboBox.comboBox.Parent = this; // Commit dataset.
columnComboBox.comboBox.DataSource = 
               new ArrayList(MyDataClass.GetArray());
columnComboBox.comboBox.DisplayMember = "name";
columnComboBox.comboBox.ValueMember = "GUID";
columnComboBox.MappingName = "country";
columnComboBox.HeaderText = "Country";
columnComboBox.Width = 200;
tableStyle.GridColumnStyles.Add(columnComboBox);

// Add the custom TableStyle to the DataGrid.
datagrid.TableStyles.Clear();
datagrid.TableStyles.Add(tableStyle);
datagrid.DataSource = table;
tableStyle.MappingName = "TableOne";

I think I have focused on a problem here: if you want a ComboBox in your DataGrid, and you want to populate the ComboBox with items from an array containing instances of your own class.

I hope someone finds it useful - enjoy.

Updated September 2006

A few bugs have been found in my source code. Apparently, someone still downloads and tries to use the source, even though .NET 2.0 has solved the problem with a ComboBox in a DataGrid. The new download contains the original source, plus a small VS project with the updated source code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

René Paw Christensen

Systems / Hardware Administrator

Denmark Denmark

Member

See http://rpc-scandinavia.dk.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to assign Combobox selcted value to another text cell in datagrid in VB.NET 2005? Pinmemberbhatiamanoj1720:56 12 Oct '09  
GeneralShow a combobox item to be selected by default PinmemberDinesh Girija Sundaram22:20 6 Jul '09  
GeneralRe: Show a combobox item to be selected by default PinmemberDIPAK@EMSYS0:18 19 Aug '09  
Generalcropping images Pinmembernace2k221:43 16 Jun '09  
Questioncombobox should hide after selecting value from combobox and selected value show in datagrid cell Pinmembernik4u20:02 21 Jan '09  
AnswerRe: combobox should hide after selecting value from combobox and selected value show in datagrid cell Pinmembernik4u22:17 21 Jan '09  
GeneralRe: combobox should hide after selecting value from combobox and selected value show in datagrid cell PinmemberRené Paw Christensen8:47 22 Jan '09  
GeneralThe Text is not empty, but the selectIndex is -1 Pinmemberdqddqq17:35 1 Nov '07  
GeneralRe: The Text is not empty, but the selectIndex is -1 Pinmemberdqddqq21:43 6 Nov '07  
GeneralWhy.... Pinmemberpatgrape18:21 30 Oct '07  
GeneralRe: Why.... PinmemberRené Paw Christensen6:35 31 Oct '07  
QuestionInsert Values dynamically? [modified] PinmemberMaYo690:11 6 Sep '07  
QuestionUpdate cell content with the selected index of the ComboBox Pinmemberlaurentma9222:59 15 Apr '07  
AnswerRe: Update cell content with the selected index of the ComboBox Pinmemberlaurentma923:31 16 Apr '07  
QuestionIt is not working PinmemberDeltaSoft19:34 10 Apr '07  
QuestionHow to get the selected value in the combobox Pinmembermax844:25 25 Mar '07  
AnswerRe: How to get the selected value in the combobox Pinmemberlaurentma923:24 16 Apr '07  
GeneralProblem with Focus on the GridComboBox Pinmemberraj30sep6:20 7 Nov '06  
GeneralNew row always has first element in combobox selected PinmemberDensi7:14 24 Oct '06  
GeneralRe: New row always has first element in combobox selected PinmemberDensi8:39 25 Oct '06  
GeneralRe: New row always has first element in combobox selected Pinmemberdqddqq20:25 1 Nov '07  
GeneralCurrentCellChanged Pinmembervaishali1021:26 17 Sep '06  
GeneralCombo Selection Change Event. Pinmemberramya.venkateswaran@gmail.com20:50 17 Sep '06  
GeneralSOURCE: Form1.cs PinmemberRené Paw Christensen5:21 12 Sep '06  
GeneralSOURCE: DataGridComboBoxColumn.cs PinmemberRené Paw Christensen5:17 12 Sep '06  
using System;
using System.Drawing;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Data;
 
namespace Test {
 
     #region DataGridComboBoxColumn
     //**********************************************************************************************
     // DataGridTextBoxColumn
     //**********************************************************************************************
     public class DataGridComboBoxColumn : DataGridColumnStyle { //DataGridTextBoxColumn {
          private DataGridComboBox combobox;
          private bool edit;
 
          //-------------------------------------------------------------------------------------------
          // Constructors and destructors
          //-------------------------------------------------------------------------------------------
          public DataGridComboBoxColumn() {
               combobox = new DataGridComboBox();
               combobox.Visible = false;
               combobox.DropDownStyle = ComboBoxStyle.DropDownList;
               combobox.Leave += new EventHandler(ComboHide);
               combobox.SelectionChangeCommitted += new EventHandler(ComboStartEditing);
               edit = false;
          } // DataGridComboBoxColumn
 
          //-------------------------------------------------------------------------------------------
          // Properties
          //-------------------------------------------------------------------------------------------
          public ComboBox comboBox {
               get {
                    return combobox;
               }
          } // comboBox
 
          //-------------------------------------------------------------------------------------------
          // ComboBox event handlers
          //-------------------------------------------------------------------------------------------
          private void ComboHide(object sender, EventArgs e) {
               // When the ComboBox looses focus, then simply hide it.
               combobox.Hide();
          } // ComboHide
 
          private void ComboStartEditing(object sender, EventArgs e) {
               // Enter edit mode.
               edit = true;
               base.ColumnStartedEditing((Control)sender);
          } // ComboStartEditing
 
          protected void GridScroll(object sender, EventArgs e) {
               combobox.Hide();
          } // GridScroll
 
          //-------------------------------------------------------------------------------------------
          // Override DataGridColumnStyle
          //-------------------------------------------------------------------------------------------
          protected override void SetDataGridInColumn(System.Windows.Forms.DataGrid value) {
               // Add the ComboBox to the DataGrids controls collection.
               // This ensures correct DataGrid scrolling.
               value.Controls.Add(combobox);
               base.SetDataGridInColumn(value);
               value.Scroll +=new EventHandler(GridScroll);
          } // SetDataGridInColumn
 
          protected override void Abort(int rowNum) {
               // Abort edit mode, discard changes and hide the ComboBox.
               edit = false;
               Invalidate();
               combobox.Hide();
          } // Abort
 
          protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {
               if (ReadOnly || DataGridTableStyle.ReadOnly)
                    return;
 
               // Setup the ComboBox for action.
               // This includes positioning the ComboBox and showing it.
               // Also select the correct item in the ComboBox before it is shown.
               combobox.Parent = this.DataGridTableStyle.DataGrid;
               combobox.Bounds = bounds;
               combobox.Size = new Size(this.Width, this.comboBox.Height);
               comboBox.SelectedValue = base.GetColumnValueAtRow(source, rowNum);//.ToString();
               combobox.Visible = (cellIsVisible == true) && (readOnly == false);
               combobox.BringToFront();
               combobox.Focus();
          } // Edit
 
          protected override bool Commit(System.Windows.Forms.CurrencyManager source, int rowNum) {
               // Commit the selected value from the ComboBox to the DataGrid.
               if (edit == true) {
                    edit = false;
                    this.SetColumnValueAtRow(source, rowNum, combobox.SelectedValue);
               }
 
               return true;
          } // Commit
 
          protected override object GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum) {
               // Return the display text associated with the data, insted of the
               // data from the DataGrid datasource.
               return combobox.GetDisplayText(base.GetColumnValueAtRow(source, rowNum));
          } // GetColumnValueAtRow
 
          protected override void SetColumnValueAtRow(CurrencyManager source, int rowNum, object value) {
               // Save the data (value) to the DataGrid datasource.
// æøå
               try {
                    base.SetColumnValueAtRow(source, rowNum, value);
               } catch {}
          } // SetColumnValueAtRow
 
          protected override int GetMinimumHeight() {
               // Return the ComboBox preferred height, plus a few pixels.
               return combobox.PreferredHeight + 2;
          } // GetMinimumHeight
 
          protected override int GetPreferredHeight(Graphics g, object val) {
               // Return the font height, plus a few pixels.
               return FontHeight + 2;
          } // GetPreferredHeight
 
          protected override Size GetPreferredSize(Graphics g, object val) {
               // Return the preferred width.
               // Iterate through all display texts in the dropdown, and measure each
               // text width.
               int widest = 0;
               SizeF stringSize = new SizeF(0, 0);
               foreach (string text in combobox.GetDisplayText()) {
                    stringSize = g.MeasureString(text, base.DataGridTableStyle.DataGrid.Font);
                    if (stringSize.Width > widest) {
                         widest = (int)Math.Ceiling(stringSize.Width);
                    }
               }
 
               return new Size(widest + 25, combobox.PreferredHeight + 2);
          } // GetPreferredSize
 
          protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum) {
               Paint(g, bounds, source, rowNum, false);
          } // Paint
 
          protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight) {
               string text = GetColumnValueAtRow(source, rowNum).ToString();
               Brush backBrush = new SolidBrush(base.DataGridTableStyle.BackColor);
               Brush foreBrush = new SolidBrush(base.DataGridTableStyle.ForeColor);
               Rectangle rect = bounds;
               StringFormat format = new StringFormat();
 
               // Handle that the row can be selected.
               if (base.DataGridTableStyle.DataGrid.IsSelected(rowNum) == true) {
                    backBrush = new SolidBrush(base.DataGridTableStyle.SelectionBackColor);
                    foreBrush = new SolidBrush(base.DataGridTableStyle.SelectionForeColor);
               }
 
               // Handle align to right.
               if (alignToRight == true) {
                    format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
               }
 
               // Handle alignment.
               switch (this.Alignment) {
                    case HorizontalAlignment.Left:
                         format.Alignment = StringAlignment.Near;
                         break;
                    case HorizontalAlignment.Right:
                         format.Alignment = StringAlignment.Far;
                         break;
                    case HorizontalAlignment.Center:
                         format.Alignment = StringAlignment.Center;
                         break;
               }
 
               // Paint.
               format.FormatFlags = StringFormatFlags.NoWrap;
               g.FillRectangle(backBrush, rect);
               rect.Offset(0, 2);
               rect.Height -= 2;
               g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect, format);
               format.Dispose();
          } // PaintText
 
     } // DataGridComboBoxColumn
     #endregion
 
     #region DataGridComboBox
     //**********************************************************************************************
     // DataGridComboBox
     //**********************************************************************************************
     public class DataGridComboBox : ComboBox {
          private const int WM_KEYUP = 0x101;
 
          protected override void WndProc(ref System.Windows.Forms.Message message) {
               // Ignore keyup to avoid problem with tabbing and dropdown list.
               if (message.Msg == WM_KEYUP) {
                    return;
               }
 
               base.WndProc(ref message);
          } // WndProc
 
          public string GetValueText(int index) {
               // Validate the index.
               if ((index < 0) && (index >= base.Items.Count))
                    throw new IndexOutOfRangeException("Invalid index.");
 
               // Get the text.
               string text = string.Empty;
               int memIndex = -1;
               try {
                    base.BeginUpdate();
                    memIndex = base.SelectedIndex;
                    base.SelectedIndex = index;
                    text = base.SelectedValue.ToString();
                    base.SelectedIndex = memIndex;
               } catch {
               } finally {
                    base.EndUpdate();
               }
 
               return text;
          } // GetValueText
 
          public string GetDisplayText(int index) {
               // Validate the index.
               if ((index < 0) && (index >= base.Items.Count))
                    throw new IndexOutOfRangeException("Invalid index.");
 
               // Get the text.
               string text = string.Empty;
               int memIndex = -1;
               try {
                    base.BeginUpdate();
                    memIndex = base.SelectedIndex;
                    base.SelectedIndex = index;
                    text = base.SelectedItem.GetType().GetProperty(base.DisplayMember).GetValue(base.SelectedItem, new object[0]).ToString();
                    base.SelectedIndex = memIndex;
               } catch {
               } finally {
                    base.EndUpdate();
               }
 
               return text;
          } // GetDisplayText
 
          public string GetDisplayText(object value) {
               // Get the text.
               string text = string.Empty;
               int memIndex = -1;
               try {
                    base.BeginUpdate();
                    memIndex = base.SelectedIndex;
                    base.SelectedValue = value;//.ToString();
                    text = base.SelectedItem.GetType().GetProperty(base.DisplayMember).GetValue(base.SelectedItem, new object[0]).ToString();
               } catch {
                    // If the value is invalid, return the first items display text.
                    return GetDisplayText(0);
               } finally {
                    base.SelectedIndex = memIndex;
                    base.EndUpdate();
               }
 
               return text;
          } // GetDisplayText
 
          public string[] GetDisplayText() {
               // Get the text.
               string[] text = new string[base.Items.Count];
               int memIndex = -1;
               try {
                    base.BeginUpdate();
                    memIndex = base.SelectedIndex;
                    for (int index = 0; index < base.Items.Count; index++) {
                         base.SelectedIndex = index;
                         text[index] = base.SelectedItem.GetType().GetProperty(base.DisplayMember).GetValue(base.SelectedItem, new object[0]).ToString();
                    }
               } catch {
               } finally {
                    base.SelectedIndex = memIndex;
                    base.EndUpdate();
               }
 
               return text;
          } // GetDisplayText
 
     } // DataGridComboBox
     #endregion
 
     #region DataGridEventColumn
     //**********************************************************************************************
     //     DataGridEventColumn
     //**********************************************************************************************
     public delegate string DataGridEventColumnEvent(CurrencyManager rowSource, int rowNum, object rowData);
 
     public class DataGridEventColumn : DataGridColumnStyle {
          private event     DataGridEventColumnEvent     eGetText          = null;
 
          //-------------------------------------------------------------------------------------------
          //     Constructors and destructors
          //-------------------------------------------------------------------------------------------
          public DataGridEventColumn() {
          } // DataGridEventColumn
 
          //-------------------------------------------------------------------------------------------
          //     events
          //-------------------------------------------------------------------------------------------
          public event DataGridEventColumnEvent EventGetText {
               add {
                    eGetText += value;
               }
               remove {
                    eGetText -= value;
               }
          } // EventGetText
 
          private string OnGetText(CurrencyManager rowSource, int rowNum, object rowData) {
               try {
                    return eGetText(rowSource, rowNum, rowData);
               } catch {
                    return string.Empty;
               }
          } // OnGetText
 
          //-------------------------------------------------------------------------------------------
          //     Override DataGridColumnStyle
          //-------------------------------------------------------------------------------------------
          protected override void Abort(int rowNum) {
          } // Abort
 
          protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {
          } // Edit
 
          protected override bool Commit(System.Windows.Forms.CurrencyManager source, int rowNum) {
               return true;
          } // Commit
 
          protected override int GetMinimumHeight() {
               // Return the ComboBox preferred height, plus a few pixels.
               return 2;
          } // GetMinimumHeight
         
          protected override int GetPreferredHeight(Graphics g, object val) {
               // Return the font height, plus a few pixels.
               return FontHeight + 2;
          } // GetPreferredHeight
 
          protected override Size GetPreferredSize(Graphics g, object val) {
               return new Size(50, FontHeight + 2);
          } // GetPreferredSize
 
          protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum) {
               Paint(g, bounds, source, rowNum, false);
          } // Paint
 
          protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight) {
               string               text                    = OnGetText(source, rowNum, GetColumnValueAtRow(source, rowNum));
               Brush                    backBrush          = new SolidBrush(base.DataGridTableStyle.BackColor);
               Brush                    foreBrush          = new SolidBrush(base.DataGridTableStyle.ForeColor);
               Rectangle          rect                    = bounds;
               StringFormat     format               = new StringFormat();
 
               // Handle that the row can be selected.
               if (base.DataGridTableStyle.DataGrid.IsSelected(rowNum) == true) {
                    backBrush          = new SolidBrush(base.DataGridTableStyle.SelectionBackColor);
                    foreBrush          = new SolidBrush(base.DataGridTableStyle.SelectionForeColor);
               }
 
               // Handle align to right.
               if (alignToRight == true) {
                    format.FormatFlags     = StringFormatFlags.DirectionRightToLeft;
               }
 
               // Handle alignment.
               switch (this.Alignment) {
                    case HorizontalAlignment.Left:
                         format.Alignment     = StringAlignment.Near;
                         break;
                    case HorizontalAlignment.Right:
                         format.Alignment     = StringAlignment.Far;
                         break;
                    case HorizontalAlignment.Center:
                         format.Alignment     = StringAlignment.Center;
                         break;
               }
 
               // Paint.
               format.FormatFlags          = StringFormatFlags.NoWrap;
               g.FillRectangle(backBrush, rect);
               rect.Offset(0, 2);
               rect.Height -= 2;
               g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect, format);
               format.Dispose();
          } // PaintText
 
     } // DataGridEventColumn
     #endregion
 
     #region BindingList
     //**********************************************************************************************
     //     BindingList
     //**********************************************************************************************
     public class BindingList : IBindingList, IComparer {
          private               DataGrid                                   datagrid                    = null;
          private event     ListChangedEventHandler          eListChanged          = null;
          private               ArrayList                              list                         = new ArrayList();
          private               PropertyDescriptor               sortProperty          = null;
          private               ListSortDirection                    sortDirection          = ListSortDirection.Ascending;
          private               ICloneable                              addNewClone               = null;
          private               Hashtable                              comparers               = new Hashtable();
 
          public BindingList(DataGrid datagrid, ICloneable addNewClone) {
               this.datagrid          = datagrid;
               this.addNewClone     = addNewClone;
          } // BindingList
 
          public BindingList(DataGrid datagrid, ICloneable addNewClone, ICollection collection) {
               this.datagrid          = datagrid;
               this.addNewClone     = addNewClone;
               list.AddRange(collection);
          } // BindingList
 
          public void SetComparer(IComparer comparer, string propertyName) {
               // Remove the comparer registed with the argumented property name.
               if (comparers.ContainsKey(propertyName) == true) {
                    comparers.Remove(propertyName);
               }
 
               // Register the argumented comparer with the argumented property name.
               if (comparer != null) {
                    comparers.Add(propertyName, comparer);
               }
          } // SetComparer
 
          #region IBindingList Members
          public event ListChangedEventHandler ListChanged {
               add {
                    eListChanged += value;
               }
               remove {
                    eListChanged -= value;
               }
          } // ListChanged
 
          private void OnListChanged(ListChangedEventArgs e) {
               try {
                    if (eListChanged != null)
                         eListChanged(this, e);
               } catch {}
          } // OnListChanged
 
          public void AddIndex(PropertyDescriptor property) {
               // The list must support this method. However, support for this method can be a nonoperation.
               sortProperty          = property;
               sortDirection          = ListSortDirection.Ascending;
          } // AddIndex
 
          public void RemoveIndex(PropertyDescriptor property) {
               // The list must support this method. However, support for this method can be a nonoperation.
               sortProperty          = null;
               sortDirection          = ListSortDirection.Ascending;
          } // RemoveIndex
 
          public bool AllowNew {
               get {
                    return true;
               }
          } // AllowNew
 
          public bool AllowEdit {
               get {
                    return true;
               }
          } // AllowEdit
 
          public bool AllowRemove {
               get {
                    return true;
               }
          } // AllowRemove
 
          public bool SupportsSorting {
               get {
                    return true;
               }
          } // SupportsSorting
 
          public bool SupportsSearching {
               get {
                    return true;
               }
          } // SupportsSearching
          public bool SupportsChangeNotification {
               get {
                    return true;
               }
          } // SupportsChangeNotification
 
          public bool IsSorted {
               get {
                    return sortProperty != null;
               }
          } // IsSorted
 
          public PropertyDescriptor SortProperty {
               get {
                    return sortProperty;
               }
          } // SortProperty
 
          public System.ComponentModel.ListSortDirection SortDirection {
               get {
                    return sortDirection;
               }
          } // SortDirection
 
          public void ApplySort(PropertyDescriptor property, System.ComponentModel.ListSortDirection direction) {
               sortProperty          = property;
               sortDirection          = direction;
 
               list.Sort(this);
               OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
          } // ApplySort
 
          public void RemoveSort() {
               sortProperty          = null;
               sortDirection          = ListSortDirection.Ascending;
 
               // Sort on the objects CompareTo.
//               list.Sort();
               OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
          } // RemoveSort
 
          public int Find(PropertyDescriptor property, object key) {
               // This method will select the first row where the value of the property parameter equals the value of the
               // key parameter.
               foreach(object obj in list) {
                    if (Compare(obj, key) == 0) {
                         return list.IndexOf(obj);
                    }
               }
               return -1;
          } // Find
 
          public object AddNew() {
               // The new object is added as the last row, which mens that the list isn't sorted anymore.
               sortProperty                    = null;
               sortDirection                    = ListSortDirection.Ascending;
 
               // Clone the object and add it.
               object     addNewObject     = addNewClone.Clone();
               int          addNewIndex          = list.Add(addNewObject);
               OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, addNewIndex));
 
               // Normally the user can regret the addition of a new row, by pressing the ESC button twice - but this
               // only works if the data objects in the list collection implements the IEditableObject interface.
               //
               // I don't like the idea that the objects used should implement some interface and perform some actions
               // which should be handled by the DataGrid class itself!
               //
               // One option is to implement and use a proxy class like BindingListRow and implement the IEditableObject
               // interface in there.
               //
               // An other option which is a trade-off, because it disables the feature where the user can regret the
               // addition of a new row, is the following few lines of code.
               if ((addNewObject is IEditableObject) == false) {
                    // Remember which cell is active and have the focus.
                    DataGridCell               activeCell     = datagrid.CurrentCell;
 
                    // ??? IT DOES THE TRICK ???. This might change the active cell.
                    OnListChanged(new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, -1));
 
                    // Refocus the correct cell.
                    datagrid.CurrentCell                         = activeCell;
               }
 
               // Return the added object.
               return addNewObject;
          } // AddNew
          #endregion
 
          #region IList Members
          public bool IsReadOnly {
               get {
                    return list.IsReadOnly;
               }
          } // IsReadOnly
 
          public bool IsFixedSize {
               get {
                    return list.IsFixedSize;
               }
          } // IsFixedSize
 
          public void Clear() {
               list.Clear();
               OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
          } // Clear
 
          public bool Contains(object value) {
               return list.Contains(value);
          } // Contains
 
          public int IndexOf(object value) {
               return list.IndexOf(value);
          } // IndexOf
 
          public object this[int index] {
               get {
                    return list[index];
               }
               set {
                    list[index] = value;
                    OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
               }
          } // this
 
          public int Add(object value) {
               int index = list.Add(value);
               OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
               return index;
          } // Add
 
          public void AddRange(ICollection c) {
               list.AddRange(c);
               OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
          } // AddRange
 
          public void Insert(int index, object value) {
               list.Insert(index, value);
               OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
          } // Insert
 
          public void Remove(object value) {
               int index = list.IndexOf(value);
               if (index > -1) {
                    list.Remove(value);
                    OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
               }
          } // Remove
 
          public void RemoveAt(int index) {
               list.RemoveAt(index);
               OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
          } // RemoveAt
          #endregion
 
          #region ICollection Members
          public bool IsSynchronized {
               get {
                    return list.IsSynchronized;
               }
          } // IsSynchronized
 
          public int Count {
               get {
                    return list.Count;
               }
          } // Count
 
          public void CopyTo(Array array, int index) {
               list.CopyTo(array, index);
          } // CopyTo
 
          public object SyncRoot {
               get {
                    return list.SyncRoot;
               }
          } // SyncRoot
          #endregion
 
          #region IEnumerable Members
          public IEnumerator GetEnumerator() {
               return list.GetEnumerator();
          } // GetEnumerator
          #endregion
 
          #region IComparer Members
          public int Compare(object x, object y) {
               int                    result          = 0;
               IComparer          comparer;
               IComparable          comparable;
               object               propertyX;
               object               propertyY;
 
               // Get the actual objects that we are comparing.
               // These are basically fields from the X and Y objects.
               propertyX     = sortProperty.GetValue(x);
               propertyY     = sortProperty.GetValue(y);
 
               // Deal with one or both being null.
               if ((propertyX == null) && (propertyY == null)) {
                    result = 0;
               } else if (propertyX == null) {
                    result = -1;
               } else if (propertyY == null) {
                    result = 1;
               } else {
                    if (comparers.ContainsKey(sortProperty.Name) == true) {
                         // Compare using the registed comparer.
                         comparer          = (IComparer)comparers[sortProperty.Name];
                         result          = comparer.Compare(propertyX, propertyY);
                    } else {
                         // Get the IComparable interface.
                         if (propertyX is IComparable) {
                              comparable     = (IComparable)propertyX;
                              result          = comparable.CompareTo(propertyY);
                         } else {
                              // Do normal comparison.
                              string.Compare(propertyX.ToString(), propertyY.ToString());
                         }
                    }
               }
 
               // If the direction is descending, reverse the sign.
               if (sortDirection == ListSortDirection.Descending) {
                    result = -result;
               }
 
               return result;
          } // Compare
          #endregion
 
     } // BindingList
     #endregion
 
     #region DataGridTableStyle
     //**********************************************************************************************
     //     DataGridTableStyle.
     //
     //     Get the datagrid combobox working with the VS designer.
     //     Code posted by Thosmos.
     //     http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/datagrid.mspx
     //**********************************************************************************************
     public class DataGridTableStyle : System.Windows.Forms.DataGridTableStyle {
 
          [Editor(typeof(DataGridColumnStylesCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
          public override System.Windows.Forms.GridColumnStylesCollection GridColumnStyles {
               get {
                    return base.GridColumnStyles;
               }
          } // GridColumnStyles
 
          private class DataGridColumnStylesCollectionEditor : System.ComponentModel.Design.CollectionEditor {
 
               public DataGridColumnStylesCollectionEditor(Type type): base(type) {
               } // DataGridColumnStylesCollectionEditor
 
               protected override Type[] CreateNewItemTypes() {
                    return new Type[] {
                         typeof(DataGridTextBoxColumn),
                         typeof(DataGridBoolColumn),
                         typeof(DataGridComboBoxColumn)
                    };
               } // CreateNewItemTypes
 
          } // DataGridColumnStylesCollectionEditor
 
     } // DataGridTableStyle
 
     public class DataGrid : System.Windows.Forms.DataGrid {
 
          [Editor(typeof(TableStylesCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
          public new System.Windows.Forms.GridTableStylesCollection TableStyles {
               get {
                    return base.TableStyles;
               }
          } // GridTableStylesCollection
 
          private class TableStylesCollectionEditor : System.ComponentModel.Design.CollectionEditor {
 
               public TableStylesCollectionEditor(Type type): base(type) {
               } // TableStylesCollectionEditor
 
               protected override Type[] CreateNewItemTypes() {
                    return new Type[] {typeof(DataGridTableStyle)};
               } // CreateNewItemTypes
 
          } // TableStylesCollectionEditor
 
     } // DataGrid
     #endregion
 
} // Test

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 13 Sep 2006
Article Copyright 2004 by René Paw Christensen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid