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;

        }    

posted @ 2009-03-02 21:25  sandy.beach  阅读(743)  评论(2编辑  收藏  举报