调用控件:
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. 开源日志系统比较:scribe、chukwa、kafka、flume

    1. 背景介绍 许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征: (1) 构建应用系统和分析系统的 ...

  2. Linux 添加ssh 公钥访问

    登陆被管理的服务器,进入需要远程登陆的用户目录,把公钥放到用户目录的 .ssh 这个目录下(如果目录不存在,需要创建~/.ssh目录,并把目录权限设置为700),把公钥改名为authorized_ke ...

  3. Inno Setup入门(十六)——Inno Setup类参考(2)

    分类: Install Setup 2013-02-02 11:28 815人阅读 评论(0) 收藏 举报 这里将接着在前面的基础上介绍如何在自定义页面上添加按钮.按钮属于Tbutton类,该类继承自 ...

  4. 解决不能打开wifi问题

    使用 命令svc wifi来调试,避免使用GUI 相关资料  https://community.freescale.com/thread/319407 D/WifiService( 313): se ...

  5. Java学习笔记之一

    1.对大小写敏感 2.class,类是构建所有Java应用程序和applet的构建块,Java应用程序中的全部内容都必须放置在类中. 3.类名,没有长度限制,必须以字母开头,可以使字母.数字.下划线的 ...

  6. C#中partial关键字

    1. 什么是局部类型? C# 2.0 引入了局部类型的概念.局部类型允许我们将一个类.结构或接口分成几个部分,分别实现在几个不同的.cs文件中. 局部类型适用于以下情况: (1) 类型特别大,不宜放在 ...

  7. 过滤器HttpModule

    1.建一个类库文件  FirsModule,实现IHttpModule接口,实现其中的两个方法,写一函数实现自己的代码逻辑,在Init方法中调用即可. // <summary> /// 第 ...

  8. zend frameword 基本语法

    #resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"#resources.frontCo ...

  9. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

  10. IP地址、子网掩码详解

    如何通过子网掩码划分网段 资料一: 一.缺省A.B.C类地址,子网掩码:  二.子网掩码的作用:  code:  IP地址 192.20.15.5 11000000 00010100 00001111 ...