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) {
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;
}
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());
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;
comboBox.DataSource = new ArrayList(MyDataClass.GetArray());
comboBox.DisplayMember = "Name"
comboBox.ValueMember = "GUID"
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 GridColumnStyle
s, enabling me to design the DataGrid
columns in the designer. This code does it manually:
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");
}
DataTable table = new DataTable("TableOne");
DataColumn column = table.Columns.Add();
column.ColumnName = "country";
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"});
DataGridTableStyle tableStyle = new DataGridTableStyle();
DataGridTextBoxColumn columnTextBox;
DataGridComboBoxColumn columnComboBox;
tableStyle.RowHeadersVisible = true;
tableStyle.RowHeaderWidth = 20;
columnTextBox = new DataGridTextBoxColumn();
columnTextBox.MappingName = "notes";
columnTextBox.HeaderText = "Country notes";
columnTextBox.Width = 200;
tableStyle.GridColumnStyles.Add(columnTextBox);
columnComboBox = new DataGridComboBoxColumn();
columnComboBox.comboBox.Parent = this;
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);
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.