分享
 
 
 

自定义控件--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;

???}

??}

?}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有