引自:梦在脚下,让心灵去飞翔
http://www.cnblogs.com/CodeAnyWhere/archive/2005/12/13/296461.html
项目需要,稍微修改了ComboBoxDataGridView
![](/images/cnblogs_com/perfect/comboBoxDataGridView1.JPG)
代码如下:
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![](/Images/OutliningIndicators/None.gif)
10
namespace WindowsApplication21
11![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
12
public class ComboBoxDataGridView : ComboBox
13![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](/Images/OutliningIndicators/ContractedSubBlock.gif)
成员变量#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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////声明一个委托
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![](/Images/OutliningIndicators/InBlock.gif)
28
#endregion
29![](/Images/OutliningIndicators/InBlock.gif)
30![](/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
31
public ComboBoxDataGridView()
32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33![](/Images/OutliningIndicators/InBlock.gif)
34
DrawDataGridView();
35
this.OnScroll += new ComboBoxDataGridViewScroll(ComboBoxDataGridView_OnScroll);
36
}
37![](/Images/OutliningIndicators/InBlock.gif)
38
public void ComboBoxDataGridView_OnScroll(object sender, bool vscroll)
39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
this.dataGridViewHost.Focus();
41
}
42
#endregion
43![](/Images/OutliningIndicators/InBlock.gif)
44![](/Images/OutliningIndicators/ContractedSubBlock.gif)
属性#region 属性
45
[Description("设置DataGridView属性"), Browsable(true), Category("N8")]
46
public DataGridView DataGridView
47![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
get
49![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
return dataGridViewHost.Control as DataGridView;
51
}
52
}
53
[Description("下拉表格尺寸是否为自动"), Browsable(true), Category("N8")]
54
public bool PopupGridAutoSize
55![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
set
57![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
m_blPopupAutoSize = value;
59
}
60
}
61
62
[Description("设置默认值"), Browsable(true), Category("N8")]
63
public string DefaultColumn
64![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
set
66![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
m_sDefaultColumn = value;
68
}
69
get
70![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
71
if (m_sDefaultColumn == null)
72![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
return String.Empty;
74
}
75
else
76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
return m_sDefaultColumn;
78
}
79
}
80
}
81
#endregion
82![](/Images/OutliningIndicators/InBlock.gif)
83![](/Images/OutliningIndicators/ContractedSubBlock.gif)
实现方法#region 实现方法
84![](/Images/OutliningIndicators/ContractedSubBlock.gif)
绘制DataGridView以及下拉DataGridView#region 绘制DataGridView以及下拉DataGridView
85
private void DrawDataGridView()
86![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/InBlock.gif)
103
dataGridViewHost = new ToolStripControlHost(dataGridView);
104
dataGridViewHost.AutoSize = m_blPopupAutoSize;
105![](/Images/OutliningIndicators/InBlock.gif)
106
dropDown = new ToolStripDropDown();
107
dropDown.Width = this.Width;
108
dropDown.Items.Add(dataGridViewHost);
109
}
110![](/Images/OutliningIndicators/InBlock.gif)
111
public void dataGridView_Click(object sender, EventArgs e)
112![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
113
PopupGridView(e);
114
}
115![](/Images/OutliningIndicators/InBlock.gif)
116
public string GetDataProperty(string sColumn) //
117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
string sValue = "";
119
if (m_dgvRow != null)
120![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
if (DataGridView.Columns.Contains(sColumn))
122![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
sValue = m_dgvRow.Cells[sColumn].Value.ToString();
124
}
125
}
126
return sValue;
127
}
128
public void dataGridView_DoubleClick(object sender, EventArgs e)
129![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
PopupGridView(e);
131
}
132![](/Images/OutliningIndicators/InBlock.gif)
133![](/Images/OutliningIndicators/ContractedSubBlock.gif)
PopupGridView#region PopupGridView
134![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
135
/// 弹出下拉表格并触发选择后事件
136
/// </summary>
137
/// <param name="e"></param>
138
private void PopupGridView(EventArgs e)
139![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
140
if (DataGridView.SelectedRows.Count > 0)
141![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142
m_dgvRow = DataGridView.SelectedRows[0];
143
if (m_sDefaultColumn != String.Empty)
144![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
string[] sColumnList = m_sDefaultColumn.Split(',');
146
foreach (string sColumn in sColumnList)
147![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
if (DataGridView.Columns.Contains(sColumn))
149![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
Items.Clear();
151
Items.Add(m_dgvRow.Cells[sColumn].Value.ToString());
152
SelectedIndex = 0;
153
}
154
}
155
}
156![](/Images/OutliningIndicators/InBlock.gif)
157
if (AfterSelector != null)
158![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
AfterSelector(this, e);
160
}
161
}
162
dropDown.Close();
163
}
164
#endregion
165
166
private void ShowDropDown()
167![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
if (dropDown != null)
169![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
170
dataGridViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
171
dropDown.Show(this, 0, this.Height);
172
}
173
}
174![](/Images/OutliningIndicators/InBlock.gif)
175![](/Images/OutliningIndicators/ContractedSubBlock.gif)
重写方法#region 重写方法
176
protected override void WndProc(ref Message m)
177![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN || m.Msg == 0x114 || m.Msg == 0x115)
179![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
ShowDropDown();
181
if (OnScroll != null)
182![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
OnScroll(this, m.Msg == 0x115);
184
}
185
return;
186
}
187
base.WndProc(ref m);
188
}
189
protected override void Dispose(bool disposing)
190![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
191
if (disposing)
192![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
193
if (dropDown != null)
194![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/None.gif)
10
namespace WindowsApplication21
11![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
12
public partial class Form2 : Form
13![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
DataTable dt = new DataTable();
15
public Form2() //构造函数
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
InitializeComponent();
18
cmbdgv.DropDownWidth = 400;
19
cmbdgv.DropDownHeight = 200;
20
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=;database=smls"))
21![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
cmbdgv.DataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
31
}
32
}
33![](/Images/OutliningIndicators/InBlock.gif)
34
public void cmbdgv_AfterSelector(object sender, EventArgs e)
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/InBlock.gif)
40
}
41
}
42
}