//Author:yinzx
//Date:2004-06-08
//Editor:UltraEdit-32
//Ref:www.codeproject.com
![](/Images/OutliningIndicators/None.gif)
//说明:前一段时间要实现如何在DataGrid中显示ComboBox,在网上找了好多文章,但我的基础较差,好多看不明白
// 但这种效果实现的原理有两种:一种是计算ComboBox的位置;另一种不用,而是继承DataGridTextBoxColumn
// 我最后采用了后一种方式,效果不错。这里只是抛砖引玉,要实现更高级的效果,还要加入一特性。
// 如有Bloger对此感兴趣,可与我联系
![](/Images/OutliningIndicators/None.gif)
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
![](/Images/OutliningIndicators/None.gif)
namespace yinzx
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
...{
public class GridComboBoxSample:System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
private DataGrid grd = new DataGrid();
[STAThread]
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Application.Run(new GridComboBoxSample());
}
public GridComboBoxSample()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.Text = "如何在DataGrid中实现ComboBox";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.MinimizeBox = this.MaximizeBox = false;
this.Controls.Add(grd);
grd.Dock = DockStyle.Fill;
//生成一个DataTable(表)
DataTable t = new DataTable("PersonInfo");
t.Columns.Add("Name",typeof(string));
t.Columns.Add("Address",typeof(string));
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
t.Rows.Add(new string[]...{"yinzx","China"});
//生成一个DataGridTableStyle(表样式)
DataGridTableStyle ts = new DataGridTableStyle();
DataGridTextBoxColumn c1 = new DataGridTextBoxColumn();
DataGridComboBoxColumn c2 = new DataGridComboBoxColumn();
c1.MappingName = c1.HeaderText = "Name";
c2.MappingName = c2.HeaderText = "Address";
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
c2.FillComboBox(new string[]...{"China","Canada","France"});
ts.MappingName = "PersonInfo";
ts.GridColumnStyles.Add(c1);
ts.GridColumnStyles.Add(c2);
//把表及表样式绑定到grd上
grd.TableStyles.Add(ts);
grd.DataSource = t;
}
}
public class DataGridComboBoxColumn:DataGridTextBoxColumn
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
private ComboBox _cbo = new ComboBox();
private CurrencyManager _source;
private int _iRowNum;
public DataGridComboBoxColumn()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
_cbo.Leave += new EventHandler(cbo_Leave);
_cbo.Visible = false;
}
public void FillComboBox(string[] cboData)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//填充数据到_cbo中
_cbo.Items.AddRange(cboData);
}
![](/Images/OutliningIndicators/InBlock.gif)
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (!this.DataGridTableStyle.DataGrid.Controls.Contains(_cbo)) this.DataGridTableStyle.DataGrid.Controls.Add(_cbo);
_iRowNum = rowNum;
_source = source;
_cbo.Bounds = bounds;//关键语句:把_cbo准确定位到相应的单元格,且不用计算位置
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
_cbo.Text = (string)this.GetColumnValueAtRow(_source, _iRowNum);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch...{}
_cbo.Visible = true;
_cbo.Focus();
}
public void cbo_Leave(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.SetColumnValueAtRow(_source,_iRowNum ,_cbo.Text);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch...{}
_cbo.Visible = false;
}
}
}