dataGrid捕获双击事件

转载 2007年09月18日 16:12:00


namespace DataGridDoubleClick
{
 using System;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data;

 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.DataGrid dataGrid1;
  private DataSet myDataSet;
  DateTime gridMouseDownTime;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   gridMouseDownTime = DateTime.Now;
   // Call SetUp to bind the controls.
   SetUp();
  }

  private void SetUp()
  {
   // Create a DataSet with two tables and one relation.
   MakeDataSet();
   /* Bind the DataGrid to the DataSet. The dataMember
   specifies that the Customers table should be displayed.*/
   dataGrid1.SetDataBinding(myDataSet, "Customers");

   //create and add a custom table style so we can
   //easily get at the behavior of a cell...
   AddCustomDataTableStyle();
  }

  private void MakeDataSet()
  {
   // Create a DataSet.
   myDataSet = new DataSet("myDataSet");
      
   // Create two DataTables.
   DataTable tCust = new DataTable("Customers");
   
   // Create two columns, and add them to the first table.
   DataColumn cCustID = new DataColumn("custID");
   DataColumn cCustName = new DataColumn("custName");
   DataColumn cCurrent = new DataColumn("custCity");
   tCust.Columns.Add(cCustID);
   tCust.Columns.Add(cCustName);
   tCust.Columns.Add(cCurrent);

   // Add the tables to the DataSet.
   myDataSet.Tables.Add(tCust);
   
   
   /* Populates the tables. For each customer and order,
   creates two DataRow variables. */
   DataRow newRow1;
   
   // Create three customers in the Customers Table.
   for(int i = 1; i < 4; i++)
   {
    newRow1 = tCust.NewRow();
    newRow1["custID"] = (100*i).ToString();
    tCust.Rows.Add(newRow1);
   }
   // Give each customer a distinct name.
   tCust.Rows[0]["custName"] = "John Summers";
   tCust.Rows[1]["custName"] = "Phil Seagram";
   tCust.Rows[2]["custName"] = "Sam Robinson";

   // And address
   tCust.Rows[0]["custCity"] = "Chicago";
   tCust.Rows[1]["custCity"] = "Los Angeles";
   tCust.Rows[2]["custCity"] = "Washington";
  }

  private void AddCustomDataTableStyle()
  {
   DataGridTableStyle ts1 = new DataGridTableStyle();
   ts1.MappingName = "Customers";
   // Set other properties.
   ts1.AlternatingBackColor = Color.LightGray;
   //
   // Add textbox column style so we can catch textbox mouse clicks
   DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
   TextCol.MappingName = "custID";
   TextCol.HeaderText = "CustomerID";
   TextCol.Width = 100;
   //add handler
   TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
   TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
   ts1.GridColumnStyles.Add(TextCol);

   TextCol = new DataGridTextBoxColumn();
   TextCol.MappingName = "custName";
   TextCol.HeaderText = "Customer Name";
   TextCol.Width = 100;
   //add handler
   TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
   TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
   ts1.GridColumnStyles.Add(TextCol);

   TextCol = new DataGridTextBoxColumn();
   TextCol.MappingName = "custCity";
   TextCol.HeaderText = "Customer Address";
   TextCol.Width = 100;
   //add handler
   TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
   TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
   ts1.GridColumnStyles.Add(TextCol);
  
    dataGrid1.TableStyles.Add(ts1);
   
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
   this.SuspendLayout();
   //
   // dataGrid1
   //
   this.dataGrid1.DataMember = "";
   this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGrid1.Location = new System.Drawing.Point(38, 26);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.Size = new System.Drawing.Size(442, 155);
   this.dataGrid1.TabIndex = 0;
   this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
   this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(508, 203);
   this.Controls.Add(this.dataGrid1);
   this.Name = "Form1";
   this.Text = "Form1";
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void TextBoxDoubleClickHandler(object sender, EventArgs e)
  {
   MessageBox.Show("TrueDoubleClick");
  }

  private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
  {
   if(DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
   {
    MessageBox.Show("GridDoubleClick");
   }
   Console.WriteLine("TextBoxMouseDownHandler  " );
  }

  private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   gridMouseDownTime = DateTime.Now;
   Console.WriteLine("dataGrid1_MouseDown  " );
  }

  private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
  {
  
  }
 }
}
 

Datagrid实现双击行事件

  • 2006年02月23日 09:05
  • 18KB
  • 下载

EasyUI中对datagrid的双击事件

昨日历史系论文盖章,这意味着大学专业课已经与我渐行渐远,有点怀念上心头,大学太快了。和几个老师聊了会,有别离的时候会发现原来老师都是这么亲切。盖章的同时,青山打个电话说道:刚哥,给你说个好消息,咱们系...

捕捉DataGrid的双击事件(C#版本)

  • 2009年11月16日 19:20
  • 6KB
  • 下载

Datagrid实现双击行事件

  • 2013年05月19日 19:23
  • 18KB
  • 下载

flex Datagrid数据列双击事件实现

最近做flex的项目遇到了一个问题:要实现双击DataGrid的一行数据,将该行数据赋值到打开的新窗口中。    在网上找了好久,发现要么答非所问,要么就是只言片语。所以我想通过CSDN这个平台把我...

GridView/DataGrid行单击和双击事件实现代码_.Net教程

功能: 单击选中行,双击打开详细页面

webview双击事件的捕获

很多时候我们需要实现双击webview全屏显示的功能,那么我们首先要捕获到webview控件的双击事件:...

Flex DataGrid双击编辑单元格

flex中的DataGrid是支持单击编辑单元格内容的,不过这确实不够方便,单击是选中数据的,怎么能变成修改呢!在网上查了看,利用一个DataGrid组件,可以实现双击修改 此组件源码为: ...

EasyUI datagrid表格双击操作的实现

在EasyUI的表格中实现双击某行执行相应操作,主要使用onDblClickRow方法,参考代码如下: var movieList; $(function() { movieLis...
  • djc777
  • djc777
  • 2016年03月31日 10:13
  • 1269
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:dataGrid捕获双击事件
举报原因:
原因补充:

(最多只允许输入30个字)