自定义控件--DataGrid中实现按输入模糊查询起相关内容,支持up,down键.

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Data.OracleClient;

using System.Windows.Forms;

namespace CommonCtl

{

?///

?/// MedicineList 的摘要说明。

?///

?public class MedicineList : System.Windows.Forms.UserControl

?{

??private System.Windows.Forms.DataGrid dataGrid1;

??private System.Windows.Forms.Panel panel1;

??private System.Windows.Forms.TextBox textBox1;

??private bool ischar=true;

??private DataSet ds = new DataSet();

??private DataView dv=null;

??private string inputcode=null;

??///

??/// 必需的设计器变量。

??///

??private System.ComponentModel.Container components = null;

??public MedicineList()

??{

???// 该调用是 Windows.Forms 窗体设计器所必需的。

???InitializeComponent();

??}

??///

??/// 清理所有正在使用的资源。

??///

??protected override void Dispose( bool disposing )

??{

???if( disposing )

???{

????if(components != null)

????{

?????components.Dispose();

????}

???}

???base.Dispose( disposing );

??}

??#region 组件设计器生成的代码

??///

??/// 设计器支持所需的方法 - 不要使用代码编辑器

??/// 修改此方法的内容。

??///

??private void InitializeComponent()

??{

???this.dataGrid1 = new System.Windows.Forms.DataGrid();

???this.panel1 = new System.Windows.Forms.Panel();

???this.textBox1 = new System.Windows.Forms.TextBox();

???((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

???this.panel1.SuspendLayout();

???this.SuspendLayout();

???//

???// dataGrid1

???//

???this.dataGrid1.AllowSorting = false;

???this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;

???this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;

???this.dataGrid1.BackgroundColor = System.Drawing.Color.LightGray;

???this.dataGrid1.CaptionBackColor = System.Drawing.Color.LightSteelBlue;

???this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;

???this.dataGrid1.CaptionText = "药品选择(拼音码)";

???this.dataGrid1.DataMember = "";

???this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;

???this.dataGrid1.FlatMode = true;

???this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);

???this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;

???this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;

???this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;

???this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);

???this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;

???this.dataGrid1.LinkColor = System.Drawing.Color.Teal;

???this.dataGrid1.Location = new System.Drawing.Point(0, 0);

???this.dataGrid1.Name = "dataGrid1";

???this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;

???this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;

???this.dataGrid1.ReadOnly = true;

???this.dataGrid1.RowHeadersVisible = false;

???this.dataGrid1.RowHeaderWidth = 10;

???this.dataGrid1.SelectionBackColor = System.Drawing.Color.Orange;

???this.dataGrid1.SelectionForeColor = System.Drawing.Color.Black;

???this.dataGrid1.Size = new System.Drawing.Size(200, 168);

???this.dataGrid1.TabIndex = 1;

???this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);

???//

???// panel1

???//

???this.panel1.Controls.Add(this.textBox1);

???this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;

???this.panel1.Location = new System.Drawing.Point(0, 168);

???this.panel1.Name = "panel1";

???this.panel1.Size = new System.Drawing.Size(200, 24);

???this.panel1.TabIndex = 2;

???//

???// textBox1

???//

???this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;

???this.textBox1.Location = new System.Drawing.Point(0, 0);

???this.textBox1.Name = "textBox1";

???this.textBox1.Size = new System.Drawing.Size(200, 21);

???this.textBox1.TabIndex = 0;

???this.textBox1.TabStop = false;

???this.textBox1.Text = "";

???this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);

???this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);

???//

???// MedicineList

???//

???this.Controls.Add(this.dataGrid1);

???this.Controls.Add(this.panel1);

???this.Name = "MedicineList";

???this.Size = new System.Drawing.Size(200, 192);

???((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

???this.panel1.ResumeLayout(false);

???this.ResumeLayout(false);

??}

??#endregion

??///

??/// 初始化

??///

??public void Init(string inputcode)

??{

???this.inputcode=inputcode;

???string cmdtext=null;

???if(inputcode=="pym") cmdtext="select ypmc as 药品名称,pym as 拼音码,wbm as 五笔码,qtm as 其他码 from yp_ypzd order by 拼音码";

???if(inputcode=="wbm") cmdtext="select ypmc as 药品名称,pym as 拼音码,wbm as 五笔码,qtm as 其他码 from yp_ypzd order by 五笔码";

???if(inputcode=="qtm") cmdtext="select ypmc as 药品名称,pym as 拼音码,wbm as 五笔码,qtm as 其他码 from yp_ypzd order by 其他码";

???ds = DBAccess.ClassDBAccess.GetData(cmdtext,"yp_hzk");

???dataGrid1.SetDataBinding(ds,"yp_hzk");

???dv = ds.Tables["yp_hzk"].DefaultView;

??}

??private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

??{

???Point pt = new Point(e.X,e.Y);

???DataGrid.HitTestInfo hit = dataGrid1.HitTest(pt);

???if(hit.Type == DataGrid.HitTestType.Cell)

???{

????dataGrid1.UnSelect(dataGrid1.CurrentRowIndex);

????dataGrid1.Select(hit.Row);

????textBox1.SelectAll();

????textBox1.Focus();

???}

??}

??private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

??{

???if(e.KeyCode==Keys.Escape){

????ischar=false;

????return;

???}

???int step=0;

???int selectrow=0;

???int allrow=dv.Count;

???if(allrow

???if(e.KeyCode==Keys.Up){

????ischar=false;

????step=-1;

???}

???if(e.KeyCode==Keys.Down){

????ischar=false;

????step=1;

???}

???selectrow=dataGrid1.CurrentRowIndex;

???if(dataGrid1.IsSelected(selectrow))

???{

????dataGrid1.UnSelect(dataGrid1.CurrentRowIndex);

???}

???selectrow=selectrow+step;

???if(selectrow

???{

????if(dv.Count0)

????{

?????dataGrid1.Select(0);

????}

????return;

???}

???if(selectrowallrow-1)

???{

????if(dv.Count0)

????{

?????dataGrid1.Select(allrow-1);

????}

????return;???

???}

???if(step!=0){

????dataGrid1.Select(selectrow);

????dataGrid1.CurrentRowIndex=selectrow;

????textBox1.Text=dataGrid1[selectrow,1].ToString();

????textBox1.SelectAll();

???}

???if(e.KeyCode==Keys.Enter){

????ischar=false;

????//返回选种的

????MessageBox.Show(dataGrid1[selectrow,1].ToString(),dataGrid1[selectrow,2].ToString());

???}

??}

??private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)

??{

???if(ischar)

???{

????if(inputcode=="pym") dv.RowFilter="拼音码 like '%"+textBox1.Text.Trim()+"%'";

????if(inputcode=="wbm") dv.RowFilter="五笔码 like '%"+textBox1.Text.Trim()+"%'";

????if(inputcode=="qtm") dv.RowFilter="其他码 like '%"+textBox1.Text.Trim()+"%'";

????

????dataGrid1.DataSource=dv;

????if(dv.Count0){

?????dataGrid1.Select(0);

????}

???}

???else

???{

????ischar=true;

???}

??}

?}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航