调用控件:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); //---------------------------------------
//设置dataWindow1属性
this.dataWindow1.PopupGridAutoSize = false;
this.dataWindow1.DropDownHeight = 300;
this.dataWindow1.DropDownWidth = 340;
this.dataWindow1.DataSource = GetDataTable();//DataTable 类型
this.dataWindow1.RowFilterVisible = false;
this.dataWindow1.AfterSelector += new EventHandler(dataWindow1_AfterSelector);
} //选择完下拉表格后执行的事件
private void dataWindow1_AfterSelector(object sender, EventArgs e)
{
textBox1.Text = dataWindow1.GetDataProperty("username");
textBox2.Text = dataWindow1.GetDataProperty("department");
textBox3.Text = dataWindow1.GetDataProperty("postion");
} 重写ComboBox控件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
namespace WindowsApplication21
{
public class DataWindow : ComboBox
{
#region 成员变量
private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
ToolStripControlHost dataGridViewHost;
ToolStripControlHost textBoxHost;
ToolStripDropDown dropDown;
private string m_sKeyWords = "";
private string m_sDisplayMember = "";
private string m_sValueMember = "";
private string m_sDisplayField = "";
private string m_Separator = "|";
private string m_NullValue = ""; private bool m_blDropShow = false;
private bool m_blPopupAutoSize = false;
private int m_SelectedIndex=-1;
public event EventHandler AfterSelector; #endregion
#region 构造函数
public DataWindow()
{
DrawDataGridView();
}
#endregion
#region 属性
[Description("空值时的默认值"), Browsable(true), Category("N8")]
public string NullValue
{
set
{
m_NullValue = value;
}
get
{
return m_NullValue;
}
}
[Description("查询关键字"), Browsable(true), Category("N8")]
public string sKeyWords
{
get
{
return m_sKeyWords;
}
set
{
m_sKeyWords = value;
}
}
[Description("文本框显示字段用逗号分割!"), Browsable(true), Category("N8")]
public string sDisplayMember
{
set
{
m_sDisplayMember = value; }
get
{
return m_sDisplayMember;
}
}
[Description("是否显示条件输入窗口!"), Browsable(true), Category("N8")]
public bool RowFilterVisible
{
set
{
dropDown.Items[0].Visible = value;
}
get
{
return dropDown.Items[0].Visible;
}
}
[Description("取值字段"), Browsable(true), Category("N8")]
public string sValueMember
{
set
{
m_sValueMember = value;
}
get
{
return m_sValueMember;
}
}
public DataView DataView
{
get
{
DataTable dataTable = GetDataTableFromDataSource();
if (dataTable == null)
{
return null;
}
return dataTable.DefaultView;
}
}
[Description("设置DataGridView属性"), Browsable(true), Category("N8")]
public DataGridView DataGridView
{
get
{
return dataGridViewHost.Control as DataGridView;
}
}
public TextBox TextBox
{
get
{
return textBoxHost.Control as TextBox;
}
}
[Description("下拉表格显示列,空为显示所有列!"), Browsable(true), Category("N8")]
public string sDisplayField
{
set
{
m_sDisplayField = value;
}
get
{
return m_sDisplayField;
}
}
[Description("数据源"), Browsable(true), Category("N8")]
public new Object DataSource
{
set
{
if (m_sDisplayField != String.Empty)
{
DataGridView.Columns.Clear();
DataGridView.AutoGenerateColumns=false;
string[] sDisplayFields = m_sDisplayField.Split(',');
foreach (string sDisplay in sDisplayFields)
{
DataGridViewTextBoxColumn dgvCell = new DataGridViewTextBoxColumn();
dgvCell.Name = sDisplay;
dgvCell.DataPropertyName = sDisplay;
DataGridView.Columns.Add(dgvCell);
}
}
DataGridView.DataSource = value;
}
get
{
return DataGridView.DataSource;
}
}
[Description("下拉表格尺寸是否为自动"), Browsable(true), Category("N8")]
public bool PopupGridAutoSize
{
set
{
m_blPopupAutoSize = value;
}
get
{
return m_blPopupAutoSize;
}
}
[Description("分割符号"), Browsable(true), Category("N8")]
public string SeparatorChar
{
set
{
m_Separator = value;
}
get
{
return m_Separator;
}
}
[Browsable(false), Bindable(true)]
public string Value
{
get
{
if (Text == String.Empty)
{
m_SelectedIndex = -1;
}
if (!String.IsNullOrEmpty(m_sValueMember))
{
if (DataView == null)
{
return Text;
}
if (m_SelectedIndex > -1)
{
object obj = DataView[m_SelectedIndex][m_sValueMember];
return obj.ToString();
}
else
{
return m_NullValue;
}
}
else
{
return Text;
}
}
set
{
int i = 0;
if (m_sValueMember == String.Empty)
{
Text = value;
}
else
{
Text = "";
if (DataView != null)
{
DataView.RowFilter = "";
foreach (DataRowView dataRowView in DataView)
{
if (dataRowView[m_sValueMember].ToString() == value)
{
m_SelectedIndex = i;
string[] sDisplayList = m_sDisplayMember.Split(',');
foreach (string sDisplay in sDisplayList)
{
if (DataGridView.Columns.Contains(sDisplay))
{
object obj = DataView[m_SelectedIndex][sDisplay];
Text += obj.ToString() + m_Separator;
}
}
Text = Text.TrimEnd('|');
break;
}
i++;
}
}
}
}
}
#endregion
#region 方法
#region 绘制DataGridView以及下拉DataGridView
private void DrawDataGridView()
{
DataGridView dataGridView = new DataGridView();
dataGridView.BackgroundColor = SystemColors.ActiveCaptionText;
dataGridView.BorderStyle = BorderStyle.None;
dataGridView.ReadOnly = true;
dataGridView.AllowUserToAddRows = false;
dataGridView.RowHeadersVisible = false;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView.DoubleClick += new EventHandler(dataGridView_DoubleClick);
dataGridView.KeyDown += new KeyEventHandler(dataGridView_KeyDown);
//设置DataGridView的数据源
Form frmDataSource = new Form();
frmDataSource.Controls.Add(dataGridView);
frmDataSource.SuspendLayout();
dataGridViewHost = new ToolStripControlHost(dataGridView);
dataGridViewHost.AutoSize = m_blPopupAutoSize;
TextBox textBox = new TextBox();
textBox.TextChanged+=new EventHandler(textBox_TextChanged);
textBox.KeyDown+=new KeyEventHandler(textBox_KeyDown);
textBoxHost = new ToolStripControlHost(textBox);
textBoxHost.AutoSize =false ;
dropDown = new ToolStripDropDown();
dropDown.Width = this.Width;
dropDown.Items.Add(textBoxHost);
dropDown.Items.Add(dataGridViewHost); }
#endregion
public string GetDataProperty(string sColumn)
{
string sValue = "";
if (DataView != null)
{
if (DataGridView.Columns.Contains(sColumn))
{
sValue = DataView[m_SelectedIndex][sColumn].ToString();
}
}
return sValue;
}
public void dataGridView_DoubleClick(object sender, EventArgs e)
{
PopupGridView(e);
}
/// <summary>
/// 弹出下拉表格并触发选择后事件
/// </summary>
/// <param name="e"></param>
private void PopupGridView(EventArgs e)
{
if (DataGridView.SelectedRows.Count > 0)
{
DataGridViewRow dgvRow = DataGridView.CurrentRow;
m_SelectedIndex = DataGridView.CurrentRow.Index;
if (m_sDisplayMember != String.Empty)
{
Text = "";
string[] sDisplayList = m_sDisplayMember.Split(',');
foreach (string sDisplay in sDisplayList)
{
if (DataGridView.Columns.Contains(sDisplay))
{
Text += dgvRow.Cells[sDisplay].Value.ToString() + m_Separator;
}
}
Text = Text.TrimEnd('|');
}
else
{
Text = dgvRow.Cells[0].Value.ToString();
}
if (AfterSelector != null)
{
AfterSelector(this,e);
}
}
dropDown.Close();
m_blDropShow=false; }
private DataTable GetDataTableFromDataSource()
{
object dataSource = DataGridView.DataSource;
return GetDataTableFromDataSource(dataSource);
}
/// <summary>
/// 从DataGridView 获取数据表
/// </summary>
/// <returns></returns>
private DataTable GetDataTableFromDataSource(object dataSource)
{
if (dataSource is DataTable)
{
return (DataTable)dataSource;
}
else if (dataSource is DataView)
{
return ((DataView)dataSource).Table;
}
else if (dataSource is BindingSource)
{
object bind = ((BindingSource)dataSource).DataSource;
if (bind is DataTable)
{
return (DataTable)bind;
}
else
{
return ((DataView)bind).Table;
}
}
else
{
return null;
}
}
private void ShowDropDown()
{
if (dropDown != null)
{
if (DataView != null)
{
DataView.RowFilter = "";
TextBox.Text = "";
textBoxHost.Width = 200;
dataGridViewHost.AutoSize = m_blPopupAutoSize;
dataGridViewHost.Size = new Size(DropDownWidth - 2, DropDownHeight);
dropDown.Show(this, 0, this.Height);
} }
}
private void dataGridView_KeyDown(object sender,KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
PopupGridView(e);
}
}
#region 重写方法 private string GetRowFilterString(string sText)
{
string sFilter = "";
if (m_sDisplayMember == String.Empty || m_sDisplayMember == null)
{
m_sDisplayMember = DataView.Table.Columns[0].ColumnName;
}
if (m_sKeyWords == String.Empty)
{
m_sKeyWords = m_sDisplayMember;
}
string[] sColumns = m_sKeyWords.Split(',');
foreach (string sColumn in sColumns)
{
sFilter += sColumn + " like " + "'%" + sText + "%'"+" or ";
}
sFilter=sFilter.Trim().TrimEnd("or".ToCharArray());
return sFilter;
}
private void textBox_TextChanged(object sender,System.EventArgs e)
{
DataView.RowFilter = GetRowFilterString(TextBox.Text);
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
PopupGridView(e);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyData == Keys.Enter)
{
DataView.RowFilter = GetRowFilterString(Text);
PopupGridView(null);
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
{
if (m_blDropShow)
{
m_blDropShow = false;
}
else
{
m_blDropShow = true;
}
if (m_blDropShow)
{
ShowDropDown();
}
else
{
dropDown.Close();
}
return;
}
base.WndProc(ref m);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (dropDown != null)
{
dropDown.Dispose();
dropDown = null;
}
}
base.Dispose(disposing);
}
#endregion
#endregion
}
}

  

Winform带dataGridview的Combox控件的更多相关文章

  1. 实现带查询功能的Combox控件

    前言 ComBox 还可以实现查询功能,通过设置 ComBox 控件的 AutoCompleteSource 属性和 AutoCompleteMode 属性,可以实现从 Combox 控件中查询已存在 ...

  2. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  3. WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用

    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...

  4. 创建一个带模版的用户控件 V.3

    再重构此篇<创建一个带模版的用户控件  V.2>http://www.cnblogs.com/insus/p/4164149.html 让其它动态实现header,Item和Footer. ...

  5. 创建一个带模版的用户控件 V.2

    前面有做练习<创建一个带模版的用户控件>http://www.cnblogs.com/insus/p/4161544.html .过于简化.通常使用数据控件Repeater会有网页写好He ...

  6. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  7. C# winform编程中多线程操作控件方法

    private void Form1_Load(object sender, EventArgs e) { Thread newthread = new Thread(new ThreadStart( ...

  8. 使用DataGridView数据窗口控件,构建用户快速输入体验

    在"随风飘散" 博客里面,介绍了一个不错的DataGridView数据窗口控件<DataGridView数据窗口控件开发方法及其源码提供下载>,这种控件在有些场合下,还 ...

  9. WinForm的延时加载控件概述

    这篇文章主要介绍了WinForm的延时加载控件,很实用的技巧,在C#程序设计中有着比较广泛的应用,需要的朋友可以参考下   本文主要针对WinForm的延迟加载在常用控件的实现做简单的描述.在进行C# ...

随机推荐

  1. 句柄C++

    C++中的句柄 这个句柄只是从英文handle翻译过来的,只问句是什么意思难以解释,这个是我从别人的空间收集的信息, 功能上的理解: 什么是"句柄"(handle),handle的 ...

  2. OpenCV——mixChannels函数

    mixChannels Copies specified channels from input arrays to the specified channels of output arrays. ...

  3. 第5章 字符串----char与String

    1.java有8种基本数据类型: 数值型:整数类型(byte,short,int,long) :浮点类型(float,double) 字符型:char  布尔型:true,false 2.char:  ...

  4. [转]Flash、Flex、AS3.0框架及类库资源收集之十全大补

    原文地址:http://www.d5power.com/portal.php?mod=view&aid=27 APIs.Libs.Components1.as3ebaylibhttp://co ...

  5. spring security 3 自定义认证,授权示例

    1,建一个web project,并导入所有需要的lib. 2,配置web.xml,使用Spring的机制装载: <?xml version="1.0" encoding=& ...

  6. MySQL-测试卷一

    MySQL-测试卷一 一.单项选择题 1 下面不属于Msql数据库特点的是(  ) A. 免费使用  B.不能跨平台  C.开源软件  D.功能强大 2 定义表的一个字段, 要求能表示4位整数,2位小 ...

  7. ActiveX控件打包成Cab实现浏览器自动下载安装

    前言 我们在浏览器中使用我们自己的一些OCX,或者是DLL这一类的文件,在X86的机器上需要我们手动将这些文件拷贝到Windows/System32 文件夹下面去,然后通过Dos命令regsvr32 ...

  8. postfix日志分析pflogsumm

    1.pflogsumm功能:统计接收.投递.转发.延时.反弹.拒绝.保留.丢弃的邮件统计发件人和收件人 统计发送和接受方主机/域名 统计SMTPD连接数...... 2.安装# yum install ...

  9. 安卓常用 widget

    验证码 public class SpinnerImg extends ImageView { /** * 完成选择后启动另外一个spinner */ private ItemListener ite ...

  10. php薪资

    2千的php程序员就是可以用cms,做一个小企业的门户网站. 3千的php程序员,可以自己写代码开发php软件,但是这样程序员写的代码非常混乱,通常只能写数千行代码的小软件,并且痛苦的完工. 4K的p ...