在DataGrid中使用ComboBox(转贴)
2005-06-01 21:12
525 查看
Download source files - 2.45 Kb
With inspiration from Alastair Stells article here on The Code Project and what else I found on the Internet, I have made the following
And
The other examples expect
Instead, I use the
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
Alastair Stells noted about this in his article:
Another issue which arose was an eye-opener! I discovered the
This means that the
I use a normal
I learned that it didn't help to show the
I think I have focused a problem here: if you want a
I hope someone finds it useful - enjoy.
Introduction
I needed aComboBoxin 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
DataGridComboBoxColumnclass.
Why did the other examples not work
All the other examples populate theComboBoxwith a
DataView, but I need to (want to be able to) populate my
ComboBoxwith 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
Nameand
GUID.
The other examples expect
columnComboBox.comboBox.DataSourceto be a
DataView, and it being an
ArrayListgenerates exceptions.
I use the ComboBox to fetch display text
Since you don't know the type ofcolumnComboBox.comboBox.DataSource, you can't use that to translate between the underlying data and what to display in the
DataGrid.
Instead, I use the
ComboBoxitself, by overriding the
ComboBoxand 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
ComboBoxitself to fetch the display text, the
ComboBoxmust be populated before the
DataGridis drawn.
Alastair Stells noted about this in his article:
Another issue which arose was an eye-opener! I discovered the
ComboBoxdoes not get populated until the
ComboBox.Visibleproperty is set for the first time.
This means that the
ComboBoxcan't be used to fetch the initial display text, because it is not visible when the
DataGridis first shown (painted).
I use a normal
ComboBoxto 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
DataSourceto the
Itemscollection.
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 inheritedDataGridTextBoxColumn, but my class then evolved into inheriting
DataGridColumnStyle. This meant that I had to implement the
Paintmethods, but at this point, I had some examples of that as well. I like the idea not having an invisible
TextBoxbehind it all.
How to use
Sadly, I don't know how to "register" myDataGridComboBoxColumnwith the
GridColumnStyles, enabling me to design the
DataGridcolumns 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
ComboBoxin your
DataGrid, and you want to populate the
ComboBoxfrom your own custom class and an
ArrayList.
I hope someone finds it useful - enjoy.
相关文章推荐
- MVVM WPF 使用 datagrid 绑定 comboBox
- 在dataGrid中使用checkBox和ComboBox的用法示例
- WPF DataGridComboBoxColumn的使用方法
- WPF使用DataGridComboBoxColumn完成绑定
- flex中ComboBox和datagrid的使用
- [WPF]DataGridTemplateColumn使用ComboBox绑定Dictionary细节记录
- WPF使用DataGridComboBoxColumn完成绑定
- WPF使用DataGridComboBoxColumn完成绑定
- 在DataGrid中使用下拉列表ComboBox控件
- DataGrid和DropDownList的一些配合以及使用css定制DataGrid (转贴)
- WPF DataGrid 中的DataGridComboBoxColumn 使用
- WPF datagrid 中 DataGridComboBoxColumn 和 DataGridCheckBoxColumn 使用方法
- flex中ComboBox和datagrid的使用
- DataGrid和DropDownList的一些配合以及使用css定制DataGrid (转贴)
- 在dataGrid中使用checkBox和ComboBox
- WPF使用DataGridComboBoxColumn完成绑定
- datagrid可编辑表格使用Combobox多选编辑时无法选择、新增时无法保存BUG解决
- jquery easyui使用combobox,datagrid,layout实例(标签实现)
- DataGrid 使用ComboBox
- FLEX在datagrid中的itemreader中渲染combobox使用outerDocument