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中添加单击或者双击事件

在网上找到过一些对于DataGrid控件的双击事件的方法,有些是需要刷新页面的,有些则太过于复杂,东搞西搞一头雾水.难啦!还是自己动手做啦,根据需求,需将DataGrid双击后将双击的行内的内容提取出...
  • qoo1983
  • qoo1983
  • 2007年05月08日 15:22
  • 4404

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

namespace DataGridDoubleClick{ using System; using System.Drawing; using System.Collections; using S...
  • abaowu
  • abaowu
  • 2004年09月13日 15:43
  • 10566

easyui datagrid 单击事件和双击事件

$('#result').datagrid({ //单击事件   onClickRow:function(rowIndex,rowData){   alert(1111); }, //双击事件 on...
  • framic
  • framic
  • 2015年10月13日 09:48
  • 8099

Easyui datagrid 双击行事件

Easyui datagrid 双击行事件: //Load 注册事件 $(function () { $("#dg_id").datagrid({ //双击事件 ...
  • heyangyi_19940703
  • heyangyi_19940703
  • 2016年09月07日 12:00
  • 5868

wpf中datagrid的双击事件

ContextMenu context = new ContextMenu(); public WStorageTankList() { ...
  • pengshumin89
  • pengshumin89
  • 2011年10月05日 15:10
  • 5027

flex Datagrid数据列双击事件实现

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

easyui 双击单元格 onDblClickCell 事件

easyui 双击单元格 onDblClickCell 想获取 datagrid 中行数据的方法: var selectedRow = $(this).datagrid(‘getSelected’...
  • zhsalo
  • zhsalo
  • 2016年12月08日 16:58
  • 2617

b-jui框架 datagrid数据添加单击事件

datagrid数据行单击事件 $.CurrentNavtab.find('#resultList_res').on('click.bjui.datagrid.tr',function(e,chec...
  • wanghe1228
  • wanghe1228
  • 2017年03月07日 14:52
  • 417

EasyUI datagrid表格双击操作的实现

在EasyUI的表格中实现双击某行执行相应操作,主要使用onDblClickRow方法,参考代码如下: var movieList; $(function() { movieLis...
  • djc777
  • djc777
  • 2016年03月31日 10:13
  • 1521

EasyUI DataGrid表格双击获取该行数据—笔记

绑定双击事件函数$("#queDg").datagrid({ ... ... ... ... onDblClickRow:function(rowIndex){ ...
  • qq_15370821
  • qq_15370821
  • 2016年12月10日 16:46
  • 4225
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:dataGrid捕获双击事件
举报原因:
原因补充:

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