在DataGrid中使用ComboBox(转贴)

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 what 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 {
   } 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 reselects 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 use 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 have 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 straight forward. 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 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.

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

   // Add customized columns.
   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   = "nameGuid";
   columnComboBox.HeaderText   = "Name";
   columnComboBox.Width     = 200;
   tableStyle.GridColumnStyles.Add(columnComboBox);

   columnTextBox       = new DataGridTextBoxColumn();
   columnTextBox.MappingName   = "textString";
   columnTextBox.HeaderText   = "Text";
   columnTextBox.Width     = 200;
   tableStyle.GridColumnStyles.Add(columnTextBox);

   // Add the custom TableStyle to the DataGrid.
   datagrid.TableStyles.Clear();
   datagrid.TableStyles.Add(tableStyle);
   datagrid.DataSource  = ..... from my database .....;
   tableStyle.MappingName  = datagrid.DataSource.GetType().Name;

I think I have focused a problem here: if you want a ComboBox in your DataGrid, and you want to populate the ComboBox from your own custom class and an ArrayList.

I hope someone finds it useful - enjoy.

posted @ 2005-06-01 21:12  吴建明  阅读(2808)  评论(1编辑  收藏  举报