在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.

转载于:https://www.cnblogs.com/wujm/archive/2005/06/01/166307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值