DataGrid的双击事件自己编写

DataGrid的单元行的双击事件的产生效果没有产生的主要原因是由于,DataGrid把一个单元格当作一个TextBox,所以没有产生双击事件

解决方法大体思想代码如下:

     DataGrid dg = this.dataGrid1;
            dg.TableStyles.Clear();
            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = "whp";
            // 设置属性    
            tableStyle.AlternatingBackColor = Color.LightGray;
            
            DataGridTextBoxColumn textStyle = new DataGridTextBoxColumn();
            textStyle.MappingName = "ID";
            textStyle.HeaderText = "ID";
            textStyle.Width = 0;
            dg.MouseDown += new System.Windows.Forms.MouseEventHandler(dg_MouseDown);

            //添加事件处理器
            textStyle.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
            textStyle.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
            tableStyle.GridColumnStyles.Add(textStyle);

            textStyle = new DataGridTextBoxColumn();
            textStyle.MappingName = "名称";
            textStyle.HeaderText = "名称";
            textStyle.Width = 660;
            

            //添加事件处理器
            textStyle.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
            textStyle.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
            tableStyle.GridColumnStyles.Add(textStyle);

            dg.TableStyles.Add(tableStyle);

            dg.DataSource =sql.CasehxmQueryResult(conditionName,likeName).Tables["whp"].DefaultView;
     

        }

        private void TextBoxDoubleClickHandler(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(dataGrid1[dataGrid1.CurrentCell.RowNumber, 0]);
            showWebBrowser(id);

        }

        private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
        {
            if (DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
            {
                int id = Convert.ToInt32(dataGrid1[dataGrid1.CurrentCell.RowNumber, 0]);
                showWebBrowser(id);

            }
            
      

        }

        private void dg_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            gridMouseDownTime = DateTime.Now;

        }    

转载于:https://www.cnblogs.com/sandybeach/archive/2009/03/02/1401776.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值