在tablepage的datagridview中,加上按列值搜索相应记录的方法

要实现的效果是:tablepage中有一个datagridview,当用户鼠标点在datagridview列标题时,浮现一个textbox,比如当前列标题是姓名,那么用户输入张三,datagridview张三那行就被定位,变成selected状态 。

具体代码如下:

首先在窗体构造函数加2个delegate

            InitializeComponent();
            dgv_compare.MouseHover+=new EventHandler(dgv_compare_MouseHover);
            dgv_compare.MouseLeave+=new EventHandler(dgv_compare_MouseLeave);

再定义一个窗体内变量表示列索引

public int gColumnIndex;

mouseHover事件

        private void dgv_compare_MouseHover(object sender, EventArgs e)
        {
            if (dgv_compare.Rows.Count == 0)   //如果纪录为0 返回
                return;
            DataGridView grid = (DataGridView)sender;
            Point clientPos = this.dgv_compare.PointToClient(Control.MousePosition);
            DataGridView.HitTestInfo hitTest = grid.HitTest(clientPos.X, clientPos.Y);
            //只在鼠标点在列标题时反应
            if (hitTest.Type == DataGridViewHitTestType.ColumnHeader)  
            {
                //DataGridViewCell cell = (DataGridViewCell)grid[hitTest.ColumnIndex, hitTest.RowIndex + 1];
                TextBox tShow = new TextBox();
                tShow.TextChanged += new EventHandler(tShow_TextChanged);                //给浮现的textbox加delegate
                this.tp_compare.Controls.Add(tShow);
                tShow.BringToFront();
                Point posInTbPage = tp_compare.PointToClient(Control.MousePosition);
                posInTbPage.X += 5;
                tShow.Location = posInTbPage;  //确定textbox的位置 
                //tShow.Location = new System.Drawing.Point(Control.MousePosition.X, Control.MousePosition.Y);
                //tShow.Location = Control.MousePosition;   //tb_rateplan_file
                tShow.Clear();
                tShow.Focus();
                gColumnIndex = hitTest.ColumnIndex;     //保存列索引
                

            }
        }

浮现的textbox的textchanged事件,根据传来的列索引查询相关的纪录

        private void tShow_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            int rowIndex = -1;
            foreach (DataGridViewRow row in dgv_compare.Rows)
            {
                string ss1 = row.Cells[gColumnIndex].Value.ToString();
                if (row.Cells[gColumnIndex].Value.ToString().Equals(tb.Text))
                {
                    rowIndex = row.Index;

                    dgv_compare.FirstDisplayedScrollingRowIndex = dgv_compare.Rows[rowIndex].Index;
                    dgv_compare.Refresh();
                    dgv_compare.Rows[rowIndex].Selected = true;

                    break;
                }
            }

        }

mouseleave事件,当鼠标离开时,使textbox不可见

        private void dgv_compare_MouseLeave(object sender, EventArgs e)
        {
            foreach (Control cl1 in tp_compare.Controls)
            {
                if (cl1.GetType() == typeof(System.Windows.Forms.TextBox))
                    cl1.Visible = false;

            }
        }

 

 

posted on 2013-02-03 13:34  齐文宣  阅读(285)  评论(0)    收藏  举报

导航