描述:
ComboBox和DataGridView两个控件制作复合控件,使用WindowForm自带控件显示详细下拉列表信息项!
用最少的代码量去实现ComBoxDataGridView代码下拉框。
效果图:

代码类:
public class ComboBoxDataGridView : ComboBox
{
//系统消息消息中的鼠标点击
private const int WINDOW_MSG_BUTTONDOWN = 0x201;
// 系统消息消息中的鼠标按下事件
private const int WINDOW_MSG_BUTTONDBCLICK = 0x203;
//存放自定义控件或 Windows 窗体控件。
private ToolStripControlHost hostDataGridView;
//Menu类似menu的容器
private ToolStripDropDown dropDown;
private int dropDownViewWidth = 0;
/// <summary>
/// 下拉列表的宽度
/// 如果如果不设置此属性:默认this.Width;
/// </summary>
public int DropDownViewWidth
{
set { dropDownViewWidth = value; }
get
{
return dropDownViewWidth == 0 ? this.Width : dropDownViewWidth;
}
}
/// <summary>
/// 构造函数(默认是空的网格)
/// </summary>
public ComboBoxDataGridView()
{
DataGridView dataGridView = new DataGridView();
InitControlView(dataGridView);
}
/// <summary>
/// 初始化树下拉框
/// </summary>
/// <param name="innerView">DataGridView</param>
public void InitControlView(DataGridView innerView)
{
if (null != innerView)
{
innerView.DoubleClick += InnerView_DoubleClick;
innerView.BorderStyle = BorderStyle.None;
innerView.Dock = DockStyle.Fill;
hostDataGridView = new ToolStripControlHost(innerView);
dropDown = new ToolStripDropDown();
dropDown.Width = DropDownViewWidth;
dropDown.Items.Clear();
dropDown.Items.Add(hostDataGridView);
}
}
/// <summary>
/// 网格控件双击选择条目后触发选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InnerView_DoubleClick(object sender, EventArgs e)
{
if (DataView.CurrentRow == null)
return;
this.Items.Clear();
this.Items.Add(DataView.CurrentRow);
this.Text = DataView.CurrentRow.Cells[GetColumnNameByDataPropertyName(this.DisplayMember)].Value.ToString();
this.Name = DataView.CurrentRow.Cells[GetColumnNameByDataPropertyName(this.DisplayMember)].Value.ToString();
this.Tag = DataView.CurrentRow.Cells[GetColumnNameByDataPropertyName(this.ValueMember)].Value.ToString();
this.Focus();
dropDown.Close();
}
/// <summary>
/// 根据数据源属性获取列名
/// </summary>
/// <param name="dataPropertyName">数据源属性:DisplayMember和ValueMember</param>
/// <returns></returns>
protected string GetColumnNameByDataPropertyName(string dataPropertyName)
{
foreach (DataGridViewColumn column in this.DataView.Columns)
{
if (column.DataPropertyName == dataPropertyName)
return column.Name;
}
return string.Empty;
}
/// <summary>
/// 设置选中的行:能区分唯一条目标识信息
/// </summary>
/// <param name="columnName">列名</param>
/// <param name="value">值:主键</param>
public void SetCurSelected(string dataPropertyName, object value)
{
foreach (DataGridViewRow row in this.DataView.Rows)
{
if (row.Cells[GetColumnNameByDataPropertyName(dataPropertyName)].Value == value)
{
//this.DataView.CurrentCell = DataView.Rows[row.Index].Cells[columnName];
row.Selected = true;
break;
}
}
this.Items.Clear();
this.Items.Add(DataView.CurrentRow);
this.SelectedIndex = 0;
this.Name = DataView.CurrentRow.Cells[GetColumnNameByDataPropertyName(this.DisplayMember)].Value.ToString();
this.Tag = DataView.CurrentRow.Cells[GetColumnNameByDataPropertyName(this.ValueMember)].Value.ToString();
}
/// <summary>
/// 获取控件内的DataGridView
/// </summary>
public DataGridView DataView
{
get { return hostDataGridView.Control as DataGridView; }
}
/// <summary>
/// 显示网格列表
/// </summary>
private void ShowDropDown()
{
if (dropDown != null)
{
hostDataGridView.Size = new Size(this.DropDownViewWidth - 2, DropDownHeight);
dropDown.Show(this, 0, this.Height);
}
}
/// <summary>
/// 处理系统消息(此处是指点击combbox时弹出下拉框)
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
if (m.Msg == WINDOW_MSG_BUTTONDBCLICK || m.Msg == WINDOW_MSG_BUTTONDOWN)
{
ShowDropDown();
return;
}
base.WndProc(ref m);
}
/// <summary>
/// 释放内存:dropDown
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (dropDown != null)
{
dropDown.Dispose();
dropDown = null;
}
}
base.Dispose(disposing);
}
}
调用方法:
//将ComboBoxDataGridView自定义控件拖放到WindowForm窗体上后,
//并且将DataGridView控件也拖放一个
List<object> lst = new List<object>();
lst.Add(new { Code = "1", Name = "姓名" });
lst.Add(new { Code = "2", Name = "性别" });
lst.Add(new { Code = "3", Name = "年龄" });
comboBoxDataGridView1.DisplayMember = "Name";
comboBoxDataGridView1.ValueMember = "Code";
this.dataGridView1.DataSource = lst;
this.Controls.Remove(this.dataGridView1);
comboBoxDataGridView1.InitControlView(this.dataGridView1);
this.dataGridView1.Refresh();
如果我们需要以检索的方式去实现过滤下拉网格出现的列表,可以增加设置触发KeyDown事件实现过滤显示功能,此处不在过多说明!
本文介绍了如何在WindowForm中创建一个组合框(ComboBox)控件,使其显示DataGridView的详细信息作为下拉列表。通过利用内置控件,实现了在ComboBox中以网格形式展示数据,从而提供更丰富的信息展示。同时提到了可以通过KeyDown事件来实现过滤搜索功能,但具体实现未详述。
1371

被折叠的 条评论
为什么被折叠?



