引自:梦在脚下,让心灵去飞翔
http://www.cnblogs.com/CodeAnyWhere/archive/2005/12/13/296461.html
项目需要,稍微修改了ComboBoxDataGridView

代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Drawing.Design;
9
10
namespace WindowsApplication21
11

{
12
public class ComboBoxDataGridView : ComboBox
13
{
14
成员变量#region 成员变量
15
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
16
ToolStripControlHost dataGridViewHost;
17
ToolStripDropDown dropDown;
18
private string m_sDefaultColumn;
19
private bool m_blPopupAutoSize = false;
20
private DataGridViewRow m_dgvRow;
21
public event EventHandler AfterSelector;
22
/**/////声明一个委托
23
public delegate void ComboBoxDataGridViewScroll(object sender, bool vscroll);
24
public event ComboBoxDataGridViewScroll OnScroll;
25
private const int WM_HSCROLL = 0x114;
26
private const int WM_VSCROLL = 0x115;
27
28
#endregion
29
30
构造函数#region 构造函数
31
public ComboBoxDataGridView()
32
{
33
34
DrawDataGridView();
35
this.OnScroll += new ComboBoxDataGridViewScroll(ComboBoxDataGridView_OnScroll);
36
}
37
38
public void ComboBoxDataGridView_OnScroll(object sender, bool vscroll)
39
{
40
this.dataGridViewHost.Focus();
41
}
42
#endregion
43
44
属性#region 属性
45
[Description("设置DataGridView属性"), Browsable(true), Category("N8")]
46
public DataGridView DataGridView
47
{
48
get
49
{
50
return dataGridViewHost.Control as DataGridView;
51
}
52
}
53
[Description("下拉表格尺寸是否为自动"), Browsable(true), Category("N8")]
54
public bool PopupGridAutoSize
55
{
56
set
57
{
58
m_blPopupAutoSize = value;
59
}
60
}
61
62
[Description("设置默认值"), Browsable(true), Category("N8")]
63
public string DefaultColumn
64
{
65
set
66
{
67
m_sDefaultColumn = value;
68
}
69
get
70
{
71
if (m_sDefaultColumn == null)
72
{
73
return String.Empty;
74
}
75
else
76
{
77
return m_sDefaultColumn;
78
}
79
}
80
}
81
#endregion
82
83
实现方法#region 实现方法
84
绘制DataGridView以及下拉DataGridView#region 绘制DataGridView以及下拉DataGridView
85
private void DrawDataGridView()
86
{
87
DataGridView dataGridView = new DataGridView();
88
dataGridView.RowHeadersVisible = false;
89
dataGridView.RowTemplate.Resizable = DataGridViewTriState.False; //固定行高不允许调节
90
dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
91
dataGridView.BackgroundColor = SystemColors.ActiveCaptionText;
92
dataGridView.BorderStyle = BorderStyle.None;
93
dataGridView.ReadOnly = true;
94
dataGridView.AllowUserToAddRows = false;
95
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
96
dataGridView.Click += new EventHandler(dataGridView_Click);
97
98
//设置DataGridView的数据源
99
Form frmDataSource = new Form();
100
frmDataSource.Controls.Add(dataGridView);
101
frmDataSource.SuspendLayout();
102
103
dataGridViewHost = new ToolStripControlHost(dataGridView);
104
dataGridViewHost.AutoSize = m_blPopupAutoSize;
105
106
dropDown = new ToolStripDropDown();
107
dropDown.Width = this.Width;
108
dropDown.Items.Add(dataGridViewHost);
109
}
110
111
public void dataGridView_Click(object sender, EventArgs e)
112
{
113
PopupGridView(e);
114
}
115
116
public string GetDataProperty(string sColumn) //
117
{
118
string sValue = "";
119
if (m_dgvRow != null)
120
{
121
if (DataGridView.Columns.Contains(sColumn))
122
{
123
sValue = m_dgvRow.Cells[sColumn].Value.ToString();
124
}
125
}
126
return sValue;
127
}
128
public void dataGridView_DoubleClick(object sender, EventArgs e)
129
{
130
PopupGridView(e);
131
}
132
133
PopupGridView#region PopupGridView
134
/**//// <summary>
135
/// 弹出下拉表格并触发选择后事件
136
/// </summary>
137
/// <param name="e"></param>
138
private void PopupGridView(EventArgs e)
139
{
140
if (DataGridView.SelectedRows.Count > 0)
141
{
142
m_dgvRow = DataGridView.SelectedRows[0];
143
if (m_sDefaultColumn != String.Empty)
144
{
145
string[] sColumnList = m_sDefaultColumn.Split(',');
146
foreach (string sColumn in sColumnList)
147
{
148
if (DataGridView.Columns.Contains(sColumn))
149
{
150
Items.Clear();
151
Items.Add(m_dgvRow.Cells[sColumn].Value.ToString());
152
SelectedIndex = 0;
153
}
154
}
155
}
156
157
if (AfterSelector != null)
158
{
159
AfterSelector(this, e);
160
}
161
}
162
dropDown.Close();
163
}
164
#endregion
165
166
private void ShowDropDown()
167
{
168
if (dropDown != null)
169
{
170
dataGridViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
171
dropDown.Show(this, 0, this.Height);
172
}
173
}
174
175
重写方法#region 重写方法
176
protected override void WndProc(ref Message m)
177
{
178
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN || m.Msg == 0x114 || m.Msg == 0x115)
179
{
180
ShowDropDown();
181
if (OnScroll != null)
182
{
183
OnScroll(this, m.Msg == 0x115);
184
}
185
return;
186
}
187
base.WndProc(ref m);
188
}
189
protected override void Dispose(bool disposing)
190
{
191
if (disposing)
192
{
193
if (dropDown != null)
194
{
195
dropDown.Dispose();
196
dropDown = null;
197
}
198
}
199
base.Dispose(disposing);
200
}
201
#endregion
202
#endregion
203
#endregion
204
}
205
}
客户端调用代码:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Data.SqlClient;
6
using System.Drawing;
7
using System.Text;
8
using System.Windows.Forms;
9
10
namespace WindowsApplication21
11

{
12
public partial class Form2 : Form
13
{
14
DataTable dt = new DataTable();
15
public Form2() //构造函数
16
{
17
InitializeComponent();
18
cmbdgv.DropDownWidth = 400;
19
cmbdgv.DropDownHeight = 200;
20
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=;database=smls"))
21
{
22
SqlDataAdapter sqlda = new SqlDataAdapter("select std_sg_code,std_text,sg_text,update_user_id from qp_standard_steel_grade", sqlconn);
23
sqlda.Fill(dt);
24
}
25
cmbdgv.DataGridView.DataSource = dt;
26
cmbdgv.DefaultColumn = "std_text"; //默认列
27
cmbdgv.AfterSelector += new EventHandler(cmbdgvAREA_NAME_AfterSelector);
28
for (int i = 0; i < cmbdgv.DataGridView.Columns.Count; i++)
29
{
30
cmbdgv.DataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
31
}
32
}
33
34
public void cmbdgv_AfterSelector(object sender, EventArgs e)
35
{
36
this.textBox1.Text = cmbdgv.GetDataProperty("std_sg_code"); //获得当前行某单元格的值
37
this.textBox2.Text = cmbdgv.GetDataProperty("sg_text");
38
this.textBox3.Text = cmbdgv.GetDataProperty("update_user_id");
39
40
}
41
}
42
}