如何用c#實現combobox下拉多列數據顯示與選擇 combobox下拉時一行顯示多列數據,選完如何只取第一列?哪位大哥,打姐教教我!萬及 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵用ComponentOne组件里的C1Combox 找现成的http://www.codeproject.com/vb/net/multicolumncombo.aspcodeproject上有的是 謝謝各位哥哥姊姊的幫忙,我找到響應的網站了;我的MSN:[email protected] 有沒有用中文寫的ComponentOne组件说明阿,我英文水平很菜! 我给你一个例子。using System;using System.Collections;using System.Data;using System.Drawing;using System.Windows.Forms;namespace EBright.Learn.Form.ComboBoxL{ /// <summary> /// Summary description for ComboBoxL. /// 存在的问题: /// - 刚进入时,输入汉字检索,不能执行 /// - 过滤时,下来操作执行两次 /// - 输入的过滤字母,只能输入一个,汉字只能一次输入 /// </summary> public class ComboBoxL:ComboBox { #region variables private System.ComponentModel.Container components = null; //确定哪些列显示出来 private int[] _displayColumns = null; //计算显示列的显示宽度 private int[] _displayWidths = null; //确定过滤的列 private int[] _filterColumns = new int[]{0} ; //数据表 private DataTable _dtSource = null; //显示的数据行 private DataRow[] _drDisplay = null; //是否是第一次初始化items private bool _needInitItem = true; //列与列之间的间隙 private int _columnSpacing = 4; private bool _dropDownOnSuggest = true; private bool _outerTextChanged = false; #endregion public ComboBoxL() { if(components == null) components = null; this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable ; } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { } #endregion #region events inherited from ComboBox protected override void OnDrawItem(DrawItemEventArgs e) { try { _outerTextChanged = false; int iIndex = e.Index; if(iIndex >= _drDisplay.Length) return; if(iIndex > -1) { int iXPos = 0; int iYPos = 0; if(_drDisplay.Length == 0) return; e.DrawBackground(); for(int index = 0; index < _dtSource.Columns.Count; index++) { if(!this.ColumnIsDisplay(index,_displayColumns)) continue; int idwIndex = GetColumnIndexOfArray(_displayColumns,index); int idw = idwIndex==-1?80:_displayWidths[idwIndex]; e.Graphics.DrawString(_drDisplay[iIndex][index].ToString(), Font, new SolidBrush(e.ForeColor), new RectangleF(iXPos, e.Bounds.Y, idw, ItemHeight)); iXPos += idw + _columnSpacing ; } iXPos = 0; iYPos += ItemHeight; e.DrawFocusRectangle(); base.OnDrawItem(e); _outerTextChanged = true; } } catch(Exception ex) { throw new Exception(ex.Message + "\r\nIn ColumnComboBox.OnDrawItem(DrawItemEventArgs)."); } } protected override void OnDropDown(EventArgs e) { if(_needInitItem) InitItems();//2006-09-21 10:12 add InitDisplay();//2006-09-21 11:50 add base.OnDropDown (e); } /*protected override void OnDropDownStyleChanged(EventArgs e) { if(DroppedDown) { } base.OnDropDownStyleChanged (e); }*/ protected override void OnTextChanged(EventArgs e) { base.OnTextChanged (e); if(Text == "'") return; _needInitItem = true; DroppedDown = false; if(_dropDownOnSuggest && !DroppedDown&&_outerTextChanged) DroppedDown = true; } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown (e); if(e.KeyCode == Keys.Escape ||//上面的控制键 e.KeyCode == Keys.F1 || e.KeyCode == Keys.F2 || e.KeyCode == Keys.F3 || e.KeyCode == Keys.F4 || e.KeyCode == Keys.F5 || e.KeyCode == Keys.F6 || e.KeyCode == Keys.F7 || e.KeyCode == Keys.F8|| e.KeyCode == Keys.F9 || e.KeyCode == Keys.F10 || e.KeyCode == Keys.F11 || e.KeyCode == Keys.F12 || e.KeyCode == Keys.Tab ||//左面及下面 e.KeyCode == Keys.CapsLock|| e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Alt|| e.KeyCode == Keys.Space || e.KeyCode == Keys.Menu || e.KeyCode == Keys.PrintScreen ||//小键盘 e.KeyCode == Keys.Scroll || e.KeyCode == Keys.Pause || e.KeyCode == Keys.Insert || e.KeyCode == Keys.Home || e.KeyCode == Keys.PageUp || e.KeyCode == Keys.End || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.NumLock || e.KeyCode == Keys.Enter ) return; if((e.KeyCode == Keys.ProcessKey&& (Text == "" || SelectedText == Text))) return; _outerTextChanged = true; if(this.SelectedText == Text) Text = e.KeyData .ToString(); } #endregion #region Property public int[] DisplayColumns { get{return _displayColumns;} set{_displayColumns = value;} } public DataTable Data { get{return _dtSource ;} set { _outerTextChanged = true; DataSource = value; _dtSource = value; _outerTextChanged = false; } } public int[] FilterColumns { get{return _filterColumns ;} set{_filterColumns = value;} } public bool DropDownOnSuggest { get{return _dropDownOnSuggest;} set{_dropDownOnSuggest = value;} } #endregion #region private methods private bool ColumnIsDisplay(int column,int[] columns) { if(column< 0) throw new Exception("column must be greater than 0.\r\n(private method)ColumnIsDisplay(int,int[])") ; if(columns == null) { columns = new int[1] ; columns[0] = 0; } else if(_dtSource != null) { if(columns.Length > _dtSource.Columns.Count) throw new Exception("DisplayColumns' count cannot be greater than count of datasource.\r\n(private method)ColumnIsDisplay(int,int[])") ; } foreach(int c in columns) { if(c == column) return true; } return false; } private int GetColumnIndexOfArray(int[] columns,int column) { int iResult = -1; if(column< 0) throw new Exception("column must be greater than 0.\r\n(private method)ColumnIsDisplay(int,int[])") ; if(_dtSource != null) { if(columns.Length > _dtSource.Columns.Count) throw new Exception("DisplayColumns' count cannot be greater than count of datasource.\r\n(private method)ColumnIsDisplay(int,int[])") ; } for(int c = 0;c<columns.Length;c++) { if(columns[c] == column) return c; } return iResult; } private void InitDisplay() { _displayWidths = new int[_displayColumns.Length>0?_displayColumns.Length:1] ; SizeF size = new SizeF(10000,ItemHeight) ; Graphics graphics = CreateGraphics(); foreach(DataRow dr in this._drDisplay) { for(int i = 0;i<this._dtSource.Columns.Count ;i++) { int iexist = this.GetColumnIndexOfArray(_displayColumns,i); if(iexist != -1) { string stmp = dr[i].ToString() ; int itmpwidth = (int)graphics.MeasureString(stmp,Font,size).Width ; if(itmpwidth > _displayWidths[iexist]) _displayWidths[iexist] = itmpwidth; } } } for(int w = 0;w < _displayWidths.Length ;w++) { _displayWidths[w] += _columnSpacing; } DropDownWidth = 1; } private void InitItems() { string text = this.Text; ArrayList al = new ArrayList() ; al.Add(_dtSource.Rows[0]); for(int f = 0;f<this._filterColumns.Length;f++) { string fcn = _dtSource.Columns[_filterColumns[f]].ColumnName ; DataRow[] drs = _dtSource.Select(fcn+" like '"+text+"%'") ; foreach(DataRow dr in drs) { if(!al.Contains(dr)) al.Add(dr); } DataRow[] drs2 = _dtSource.Select(fcn+" like '_"+text+"%'") ; foreach(DataRow dr in drs2) { if(!al.Contains(dr)) al.Add(dr); } DataRow[] drs3 = _dtSource.Select(fcn+" like '%"+text+"%'") ; foreach(DataRow dr in drs3) { if(!al.Contains(dr)) al.Add(dr); } } _drDisplay = new DataRow[al.Count] ; al.CopyTo(_drDisplay) ; //将过滤后的数据,赋给DataSource,2006-09-21 10:08 DataTable dt = this._dtSource.Clone() ; foreach(DataRow dr in _drDisplay) { dt.ImportRow(dr) ; } _outerTextChanged = false;//2006-09-21 13:06 DataSource = dt; _outerTextChanged = true; } #endregion }} 上面代码是重写了ComboBox的一些事件,ComboBoxL.cs下面是调用示例:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace EBright.Learn.Test_ComboBoxL{ /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private EBright.Learn.Form.ComboBoxL.ComboBoxL comboBoxL1; private System.Windows.Forms.ComboBox comboBox1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); Init(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.comboBoxL1 = new EBright.Learn.Form.ComboBoxL.ComboBoxL(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBoxL1 // this.comboBoxL1.BackColor = System.Drawing.Color.AliceBlue; this.comboBoxL1.Data = null; this.comboBoxL1.DisplayColumns = null; this.comboBoxL1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.comboBoxL1.FilterColumns = new int[] { 0}; this.comboBoxL1.ForeColor = System.Drawing.Color.DarkBlue; this.comboBoxL1.Location = new System.Drawing.Point(64, 48); this.comboBoxL1.Name = "comboBoxL1"; this.comboBoxL1.Size = new System.Drawing.Size(232, 22); this.comboBoxL1.TabIndex = 0; // // comboBox1 // this.comboBox1.BackColor = System.Drawing.Color.AliceBlue; this.comboBox1.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.comboBox1.ForeColor = System.Drawing.SystemColors.HotTrack; this.comboBox1.Items.AddRange(new object[] { "aaaa", "bbbb", "cccc", "dddd"}); this.comboBox1.Location = new System.Drawing.Point(64, 136); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(216, 20); this.comboBox1.TabIndex = 1; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(504, 256); this.Controls.Add(this.comboBox1); this.Controls.Add(this.comboBoxL1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Init() { #region 数据 //构造数据表 DataTable dt = new DataTable() ; dt.Columns.Add("CODE") ; dt.Columns.Add("NAME") ; dt.Columns.Add("SCODE") ; DataRow dr0 = dt.NewRow() ; dr0["CODE"] = 0; dr0["NAME"] = "深圳华为"; dr0["SCODE"] = "szhw"; dt.Rows.Add(dr0); DataRow dr1 = dt.NewRow() ; dr1["CODE"] = 1; dr1["NAME"] = "青岛啤酒"; dr1["SCODE"] = "qdpj"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow() ; dr2["CODE"] = 2; dr2["NAME"] = "安徽奇瑞"; dr2["SCODE"] = "ahqr"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow() ; dr3["CODE"] = 3; dr3["NAME"] = "北京国际"; dr3["SCODE"] = "bjgj"; dt.Rows.Add(dr3); DataRow dr4 = dt.NewRow() ; dr4["CODE"] = 4; dr4["NAME"] = "北京奥运"; dr4["SCODE"] = "bjay"; dt.Rows.Add(dr4); DataRow dr5 = dt.NewRow() ; dr5["CODE"] = 5; dr5["NAME"] = "重庆解答"; dr5["SCODE"] = "cqjd"; dt.Rows.Add(dr5); DataRow dr6 = dt.NewRow() ; dr6["CODE"] = 6; dr6["NAME"] = "大中华"; dr6["SCODE"] = "dzh"; dt.Rows.Add(dr6); DataRow dr7 = dt.NewRow() ; dr7["CODE"] = 7; dr7["NAME"] = "儿童节"; dr7["SCODE"] = "etj"; dt.Rows.Add(dr7); DataRow dr8 = dt.NewRow() ; dr8["CODE"] = 8; dr8["NAME"] = "伏天汽车"; dr8["SCODE"] = "ftqc"; dt.Rows.Add(dr8); DataRow dr9 = dt.NewRow() ; dr9["CODE"] = 9; dr9["NAME"] = "共北口岸"; dr9["SCODE"] = "gbka"; dt.Rows.Add(dr9); DataRow dr10 = dt.NewRow() ; dr10["CODE"] = 10; dr10["NAME"] = "河南面条"; dr10["SCODE"] = "hnmt"; dt.Rows.Add(dr10); DataRow dr11 = dt.NewRow() ; dr11["CODE"] = 11; dr11["NAME"] = "一为棵"; dr11["SCODE"] = "iwk"; dt.Rows.Add(dr11); DataRow dr12 = dt.NewRow() ; dr12["CODE"] = 12; dr12["NAME"] = "济南大饼"; dr12["SCODE"] = "jndb"; dt.Rows.Add(dr12); DataRow dr13 = dt.NewRow() ; dr13["CODE"] = 13; dr13["NAME"] = "开山桃"; dr13["SCODE"] = "kst"; dt.Rows.Add(dr13); DataRow dr14 = dt.NewRow() ; dr14["CODE"] = 14; dr14["NAME"] = "珠江啤酒"; dr14["SCODE"] = "zjpj"; dt.Rows.Add(dr14); DataRow dr15 = dt.NewRow() ; dr15["CODE"] = 15; dr15["NAME"] = "芜湖小吃"; dr15["SCODE"] = "whxc"; dt.Rows.Add(dr15); DataRow dr16 = dt.NewRow() ; dr16["CODE"] = 16; dr16["NAME"] = "西江月"; dr16["SCODE"] = "xjy"; dt.Rows.Add(dr16); DataRow dr17 = dt.NewRow() ; dr17["CODE"] = 17; dr17["NAME"] = "其山老妖b"; dr17["SCODE"] = "qsly"; dt.Rows.Add(dr17); DataRow drx = dt.NewRow() ; drx["CODE"] = -1; drx["NAME"] = ""; drx["SCODE"] = ""; dt.Rows.InsertAt(drx,0) ; #endregion this.comboBoxL1.Data = dt; this.comboBoxL1.ValueMember = "CODE"; this.comboBoxL1.DisplayMember = "NAME"; this.comboBoxL1.DisplayColumns = new int[]{1,2} ; this.comboBoxL1.FilterColumns = new int[]{1,2} ; //this.comboBox1.DataSource = dt; //this.comboBox1.ValueMember = "CODE"; //this.comboBox1.DisplayMember = "NAME"; } }} http://www.cnblogs.com/lovvver/archive/2006/09/30/519408.html VS2010窗体怎么突破大小限制(严重) 关于Lambda表达式 请问这种需求能否实现? 数据挖掘? 如何从内存中读取数据变量的值 C#中如何改变对话框的标题栏颜色? C#中最简单验证数据是否为日期或数字的方法是什么? 一个关于winform控件外观问题! 这是什么错误?安装有关 这个问题可能很少有人碰到,不知是否有人能够解决? combobox绑定多列数据 新手报道~~放点分
http://www.codeproject.com/vb/net/multicolumncombo.asp
codeproject上有的是
我的MSN:[email protected]
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Windows.Forms;namespace EBright.Learn.Form.ComboBoxL
{
/// <summary>
/// Summary description for ComboBoxL.
/// 存在的问题:
/// - 刚进入时,输入汉字检索,不能执行
/// - 过滤时,下来操作执行两次
/// - 输入的过滤字母,只能输入一个,汉字只能一次输入
/// </summary>
public class ComboBoxL:ComboBox
{
#region variables
private System.ComponentModel.Container components = null;
//确定哪些列显示出来
private int[] _displayColumns = null;
//计算显示列的显示宽度
private int[] _displayWidths = null;
//确定过滤的列
private int[] _filterColumns = new int[]{0} ;
//数据表
private DataTable _dtSource = null;
//显示的数据行
private DataRow[] _drDisplay = null;
//是否是第一次初始化items
private bool _needInitItem = true;
//列与列之间的间隙
private int _columnSpacing = 4;
private bool _dropDownOnSuggest = true;
private bool _outerTextChanged = false; #endregion
public ComboBoxL()
{
if(components == null)
components = null;
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable ;
} #region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
} #endregion
protected override void OnDrawItem(DrawItemEventArgs e)
{
try
{
_outerTextChanged = false;
int iIndex = e.Index;
if(iIndex >= _drDisplay.Length)
return;
if(iIndex > -1)
{
int iXPos = 0;
int iYPos = 0; if(_drDisplay.Length == 0)
return;
e.DrawBackground();
for(int index = 0; index < _dtSource.Columns.Count; index++)
{
if(!this.ColumnIsDisplay(index,_displayColumns))
continue;
int idwIndex = GetColumnIndexOfArray(_displayColumns,index);
int idw = idwIndex==-1?80:_displayWidths[idwIndex];
e.Graphics.DrawString(_drDisplay[iIndex][index].ToString(), Font, new SolidBrush(e.ForeColor), new RectangleF(iXPos, e.Bounds.Y, idw, ItemHeight));
iXPos += idw + _columnSpacing ;
}
iXPos = 0;
iYPos += ItemHeight;
e.DrawFocusRectangle();
base.OnDrawItem(e);
_outerTextChanged = true;
}
}
catch(Exception ex)
{
throw new Exception(ex.Message + "\r\nIn ColumnComboBox.OnDrawItem(DrawItemEventArgs).");
}
} protected override void OnDropDown(EventArgs e)
{
if(_needInitItem)
InitItems();//2006-09-21 10:12 add
InitDisplay();//2006-09-21 11:50 add
base.OnDropDown (e);
}
/*protected override void OnDropDownStyleChanged(EventArgs e)
{
if(DroppedDown)
{
}
base.OnDropDownStyleChanged (e);
}*/ protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);
if(Text == "'")
return;
_needInitItem = true;
DroppedDown = false;
if(_dropDownOnSuggest && !DroppedDown&&_outerTextChanged)
DroppedDown = true;
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown (e);
if(e.KeyCode == Keys.Escape ||//上面的控制键
e.KeyCode == Keys.F1 ||
e.KeyCode == Keys.F2 ||
e.KeyCode == Keys.F3 ||
e.KeyCode == Keys.F4 ||
e.KeyCode == Keys.F5 ||
e.KeyCode == Keys.F6 ||
e.KeyCode == Keys.F7 ||
e.KeyCode == Keys.F8||
e.KeyCode == Keys.F9 ||
e.KeyCode == Keys.F10 ||
e.KeyCode == Keys.F11 ||
e.KeyCode == Keys.F12 ||
e.KeyCode == Keys.Tab ||//左面及下面
e.KeyCode == Keys.CapsLock||
e.KeyCode == Keys.ShiftKey ||
e.KeyCode == Keys.ControlKey ||
e.KeyCode == Keys.Alt||
e.KeyCode == Keys.Space ||
e.KeyCode == Keys.Menu ||
e.KeyCode == Keys.PrintScreen ||//小键盘
e.KeyCode == Keys.Scroll ||
e.KeyCode == Keys.Pause ||
e.KeyCode == Keys.Insert ||
e.KeyCode == Keys.Home ||
e.KeyCode == Keys.PageUp ||
e.KeyCode == Keys.End ||
e.KeyCode == Keys.PageDown ||
e.KeyCode == Keys.Up ||
e.KeyCode == Keys.Down ||
e.KeyCode == Keys.Left ||
e.KeyCode == Keys.Right ||
e.KeyCode == Keys.NumLock ||
e.KeyCode == Keys.Enter
)
return;
if((e.KeyCode == Keys.ProcessKey&& (Text == "" || SelectedText == Text)))
return;
_outerTextChanged = true;
if(this.SelectedText == Text)
Text = e.KeyData .ToString();
}
#endregion #region Property
public int[] DisplayColumns
{
get{return _displayColumns;}
set{_displayColumns = value;}
}
public DataTable Data
{
get{return _dtSource ;}
set
{
_outerTextChanged = true;
DataSource = value;
_dtSource = value;
_outerTextChanged = false;
}
}
public int[] FilterColumns
{
get{return _filterColumns ;}
set{_filterColumns = value;}
}
public bool DropDownOnSuggest
{
get{return _dropDownOnSuggest;}
set{_dropDownOnSuggest = value;}
}
#endregion #region private methods
private bool ColumnIsDisplay(int column,int[] columns)
{
if(column< 0)
throw new Exception("column must be greater than 0.\r\n(private method)ColumnIsDisplay(int,int[])") ;
if(columns == null)
{
columns = new int[1] ;
columns[0] = 0;
}
else if(_dtSource != null)
{
if(columns.Length > _dtSource.Columns.Count)
throw new Exception("DisplayColumns' count cannot be greater than count of datasource.\r\n(private method)ColumnIsDisplay(int,int[])") ;
}
foreach(int c in columns)
{
if(c == column)
return true;
}
return false;
}
private int GetColumnIndexOfArray(int[] columns,int column)
{
int iResult = -1;
if(column< 0)
throw new Exception("column must be greater than 0.\r\n(private method)ColumnIsDisplay(int,int[])") ;
if(_dtSource != null)
{
if(columns.Length > _dtSource.Columns.Count)
throw new Exception("DisplayColumns' count cannot be greater than count of datasource.\r\n(private method)ColumnIsDisplay(int,int[])") ;
}
for(int c = 0;c<columns.Length;c++)
{
if(columns[c] == column)
return c;
}
return iResult;
}
private void InitDisplay()
{
_displayWidths = new int[_displayColumns.Length>0?_displayColumns.Length:1] ;
SizeF size = new SizeF(10000,ItemHeight) ;
Graphics graphics = CreateGraphics();
foreach(DataRow dr in this._drDisplay)
{
for(int i = 0;i<this._dtSource.Columns.Count ;i++)
{
int iexist = this.GetColumnIndexOfArray(_displayColumns,i);
if(iexist != -1)
{
string stmp = dr[i].ToString() ;
int itmpwidth = (int)graphics.MeasureString(stmp,Font,size).Width ;
if(itmpwidth > _displayWidths[iexist])
_displayWidths[iexist] = itmpwidth;
}
}
}
for(int w = 0;w < _displayWidths.Length ;w++)
{
_displayWidths[w] += _columnSpacing;
}
DropDownWidth = 1;
}
private void InitItems()
{
string text = this.Text;
ArrayList al = new ArrayList() ;
al.Add(_dtSource.Rows[0]);
for(int f = 0;f<this._filterColumns.Length;f++)
{
string fcn = _dtSource.Columns[_filterColumns[f]].ColumnName ;
DataRow[] drs = _dtSource.Select(fcn+" like '"+text+"%'") ;
foreach(DataRow dr in drs)
{
if(!al.Contains(dr))
al.Add(dr);
}
DataRow[] drs2 = _dtSource.Select(fcn+" like '_"+text+"%'") ;
foreach(DataRow dr in drs2)
{
if(!al.Contains(dr))
al.Add(dr);
}
DataRow[] drs3 = _dtSource.Select(fcn+" like '%"+text+"%'") ;
foreach(DataRow dr in drs3)
{
if(!al.Contains(dr))
al.Add(dr);
}
}
_drDisplay = new DataRow[al.Count] ;
al.CopyTo(_drDisplay) ;
//将过滤后的数据,赋给DataSource,2006-09-21 10:08
DataTable dt = this._dtSource.Clone() ;
foreach(DataRow dr in _drDisplay)
{
dt.ImportRow(dr) ;
}
_outerTextChanged = false;//2006-09-21 13:06
DataSource = dt;
_outerTextChanged = true;
}
#endregion
}
}
下面是调用示例:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace EBright.Learn.Test_ComboBoxL
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private EBright.Learn.Form.ComboBoxL.ComboBoxL comboBoxL1;
private System.Windows.Forms.ComboBox comboBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); Init();
//
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBoxL1 = new EBright.Learn.Form.ComboBoxL.ComboBoxL();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBoxL1
//
this.comboBoxL1.BackColor = System.Drawing.Color.AliceBlue;
this.comboBoxL1.Data = null;
this.comboBoxL1.DisplayColumns = null;
this.comboBoxL1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.comboBoxL1.FilterColumns = new int[] {
0};
this.comboBoxL1.ForeColor = System.Drawing.Color.DarkBlue;
this.comboBoxL1.Location = new System.Drawing.Point(64, 48);
this.comboBoxL1.Name = "comboBoxL1";
this.comboBoxL1.Size = new System.Drawing.Size(232, 22);
this.comboBoxL1.TabIndex = 0;
//
// comboBox1
//
this.comboBox1.BackColor = System.Drawing.Color.AliceBlue;
this.comboBox1.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.comboBox1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.comboBox1.Items.AddRange(new object[] {
"aaaa",
"bbbb",
"cccc",
"dddd"});
this.comboBox1.Location = new System.Drawing.Point(64, 136);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(216, 20);
this.comboBox1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 256);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.comboBoxL1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Init()
{
#region 数据
//构造数据表
DataTable dt = new DataTable() ;
dt.Columns.Add("CODE") ;
dt.Columns.Add("NAME") ;
dt.Columns.Add("SCODE") ;
DataRow dr0 = dt.NewRow() ;
dr0["CODE"] = 0;
dr0["NAME"] = "深圳华为";
dr0["SCODE"] = "szhw";
dt.Rows.Add(dr0); DataRow dr1 = dt.NewRow() ;
dr1["CODE"] = 1;
dr1["NAME"] = "青岛啤酒";
dr1["SCODE"] = "qdpj";
dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow() ;
dr2["CODE"] = 2;
dr2["NAME"] = "安徽奇瑞";
dr2["SCODE"] = "ahqr";
dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow() ;
dr3["CODE"] = 3;
dr3["NAME"] = "北京国际";
dr3["SCODE"] = "bjgj";
dt.Rows.Add(dr3); DataRow dr4 = dt.NewRow() ;
dr4["CODE"] = 4;
dr4["NAME"] = "北京奥运";
dr4["SCODE"] = "bjay";
dt.Rows.Add(dr4); DataRow dr5 = dt.NewRow() ;
dr5["CODE"] = 5;
dr5["NAME"] = "重庆解答";
dr5["SCODE"] = "cqjd";
dt.Rows.Add(dr5); DataRow dr6 = dt.NewRow() ;
dr6["CODE"] = 6;
dr6["NAME"] = "大中华";
dr6["SCODE"] = "dzh";
dt.Rows.Add(dr6); DataRow dr7 = dt.NewRow() ;
dr7["CODE"] = 7;
dr7["NAME"] = "儿童节";
dr7["SCODE"] = "etj";
dt.Rows.Add(dr7); DataRow dr8 = dt.NewRow() ;
dr8["CODE"] = 8;
dr8["NAME"] = "伏天汽车";
dr8["SCODE"] = "ftqc";
dt.Rows.Add(dr8); DataRow dr9 = dt.NewRow() ;
dr9["CODE"] = 9;
dr9["NAME"] = "共北口岸";
dr9["SCODE"] = "gbka";
dt.Rows.Add(dr9); DataRow dr10 = dt.NewRow() ;
dr10["CODE"] = 10;
dr10["NAME"] = "河南面条";
dr10["SCODE"] = "hnmt";
dt.Rows.Add(dr10); DataRow dr11 = dt.NewRow() ;
dr11["CODE"] = 11;
dr11["NAME"] = "一为棵";
dr11["SCODE"] = "iwk";
dt.Rows.Add(dr11); DataRow dr12 = dt.NewRow() ;
dr12["CODE"] = 12;
dr12["NAME"] = "济南大饼";
dr12["SCODE"] = "jndb";
dt.Rows.Add(dr12); DataRow dr13 = dt.NewRow() ;
dr13["CODE"] = 13;
dr13["NAME"] = "开山桃";
dr13["SCODE"] = "kst";
dt.Rows.Add(dr13); DataRow dr14 = dt.NewRow() ;
dr14["CODE"] = 14;
dr14["NAME"] = "珠江啤酒";
dr14["SCODE"] = "zjpj";
dt.Rows.Add(dr14); DataRow dr15 = dt.NewRow() ;
dr15["CODE"] = 15;
dr15["NAME"] = "芜湖小吃";
dr15["SCODE"] = "whxc";
dt.Rows.Add(dr15); DataRow dr16 = dt.NewRow() ;
dr16["CODE"] = 16;
dr16["NAME"] = "西江月";
dr16["SCODE"] = "xjy";
dt.Rows.Add(dr16); DataRow dr17 = dt.NewRow() ;
dr17["CODE"] = 17;
dr17["NAME"] = "其山老妖b";
dr17["SCODE"] = "qsly";
dt.Rows.Add(dr17); DataRow drx = dt.NewRow() ;
drx["CODE"] = -1;
drx["NAME"] = "";
drx["SCODE"] = "";
dt.Rows.InsertAt(drx,0) ; #endregion
this.comboBoxL1.Data = dt;
this.comboBoxL1.ValueMember = "CODE";
this.comboBoxL1.DisplayMember = "NAME";
this.comboBoxL1.DisplayColumns = new int[]{1,2} ;
this.comboBoxL1.FilterColumns = new int[]{1,2} ; //this.comboBox1.DataSource = dt;
//this.comboBox1.ValueMember = "CODE";
//this.comboBox1.DisplayMember = "NAME";
}
}
}