`
leonardleonard
  • 浏览: 821538 次
社区版块
存档分类
最新评论

捕捉DataGrid的双击事件

阅读更多
namespace DataGridDoubleClick
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet myDataSet;
DateTime gridMouseDownTime;
private System.Windows.Forms.Label label1;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
gridMouseDownTime = DateTime.Now;
SetUp();
}

private void SetUp()
{
// 用2个Table和1和Relation创建DataSet
MakeDataSet();
// 数据绑定
dataGrid1.SetDataBinding(myDataSet, "Customers");

//添加样式
AddCustomDataTableStyle();
}

private void MakeDataSet()
{
// 创建DataSet.
myDataSet = new DataSet("myDataSet");

// 创建2个DataTables.
DataTable tCust = new DataTable("Customers");

// 创建两个列,并添加到第一个表
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);

// 把tables添加到DataSet.
myDataSet.Tables.Add(tCust);


/* 计算tables.对每个客户,创建DataRow变量 */
DataRow newRow1;

// 添加记录到 Customers Table.
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = (100*i).ToString();
tCust.Rows.Add(newRow1);
}

tCust.Rows[0]["custName"] = "【孟宪会之精彩世界】";
tCust.Rows[1]["custName"] = "net_lover";
tCust.Rows[2]["custName"] = "http://xml.sz.luohuedu.net/";


tCust.Rows[0]["custCity"] = "北京";
tCust.Rows[1]["custCity"] = "上海";
tCust.Rows[2]["custCity"] = "河南";
}

private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Customers";
// 设置属性
ts1.AlternatingBackColor = Color.LightGray;

// 添加Textbox列样式,以便我们捕捉鼠标事件
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custID";
TextCol.HeaderText = "序号";
TextCol.Width = 100;

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

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName";
TextCol.HeaderText = "姓名";
TextCol.Width = 100;
//添加事件处理器
TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custCity";
TextCol.HeaderText = "地址";
TextCol.Width = 100;
//添加事件处理器
TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);

dataGrid1.TableStyles.Add(ts1);

}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Info;
this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.WindowText;
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(11, 9);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(368, 144);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
//
// label1
//
this.label1.Location = new System.Drawing.Point(4, 166);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(383, 23);
this.label1.TabIndex = 1;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.Form1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(387, 201);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "鼠标双击事件的例子";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
MessageBox.Show("双击事件发生。鼠标双击到的值:"+((TextBox)sender).Text.ToString());
}

private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
{
if(DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
{
MessageBox.Show("双击事件发生。鼠标双击到的值:"+((TextBox)sender).Text.ToString());
}
label1.Text = "TextBox 鼠标按下了。 ";
}

private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
gridMouseDownTime = DateTime.Now;
label1.Text = "DataGrid1 鼠标按下了。 ";
}

private void Form1_Click(object sender, System.EventArgs e)
{
label1.Text="";
}
private void label1_Click(object sender, System.EventArgs e)
{
label1.Text="";
}
}


 
分享到:
评论

相关推荐

    DataGrid双击事件&取值

    本篇文章将深入探讨如何在Visual Studio 2012中利用`DataGrid`控件实现双击事件处理以及如何从选定的行中获取数据。 首先,我们需要了解`DataGrid`控件的基本用法。`DataGrid`可以与各种数据源绑定,如数组、...

    C#实现给DataGrid单元行添加双击事件的方法

    在这个特定的场景中,我们关注的是如何为DataGrid的每一行添加双击事件,以便在用户双击某一行时触发特定的操作,如弹出删除确认对话框。以下是对这一主题的详细解释。 首先,我们需要了解的是在ASP.NET中,...

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

    ### 捕捉DataGrid的双击事件(C#版本) #### 概述 在本篇文章中,我们将探讨如何在ASP.NET应用程序中捕获DataGrid控件的双击事件。DataGrid是一个非常强大的ASP.NET服务器控件,用于显示、编辑、插入和删除数据。...

    Datagrid实现双击行事件

    本篇将深入探讨如何实现`DataGrid`控件的双击行事件,以便在用户双击某一行时触发特定的操作。 首先,我们要理解`DataGrid`的事件模型。`DataGrid`控件有多种内置事件,其中包括`CellDoubleClick`和`RowDoubleClick...

    silverlight dataGrid 模拟双击一行事件

    在本场景中,我们关注的是如何在Silverlight 4环境中,利用Visual Studio 2010和Windows XP系统,实现DataGrid控件对用户双击某一行时触发特定的事件。下面将详细介绍这一功能的实现过程及其相关的知识点。 首先,...

    Datagrid键盘事件响应

    本文将详细讲解Datagrid键盘事件响应,包括Datagrid整体和单元格两个层面。 首先,我们来看Datagrid上的键盘响应。当Datagrid获得焦点后,用户可以通过键盘进行操作。例如,按下"0-1"、"A-Z"、"Enter"、"Backspace...

    ASP.NET GridView 和GridData行单击与双击事件

    本主题将深入探讨如何处理GridView和GridData控件的行单击与双击事件,以实现弹出详细页面和编辑状态更新的功能。 首先,GridView控件是一个强大的数据绑定控件,它能够从数据库或其他数据源动态生成表格格式的数据...

    Flex4 DataGrid表格操作

    Flex4 DataGrid表格操作,表格回车事件默认是换行,这里修改为换单元格,单元格轮完换行。支持上下左右和回车键控制光标位置,还添加了"+","-"按钮,用于控制添加行和删除选中的行。

    c# WPF中通过双击编辑DataGrid中Cell的示例(附源码).docx

    在C# WPF开发中,有时我们需要实现DataGrid控件的单元格(Cell)双击编辑功能,以便用户可以直接在表格内编辑数据并保存。原生的WPF DataGrid并未直接提供这一特性,但可以通过扩展或第三方库实现。在这个示例中,...

    ASP.net GridView双击事件,弹出一个窗口显示详细信息

    本主题将深入探讨如何利用ASP.NET GridView的双击事件来实现用户双击某行时,弹出一个窗口显示该行的详细信息。 首先,我们需要在ASP.NET页面上添加一个GridView控件,并将其与数据源(如SqlDataSource或...

    datagrid curentchange事件

    ### datagrid currentchange事件 #### 一、简介 在软件开发过程中,特别是在Windows Forms应用程序中,数据绑定是一项非常重要的功能。`datagrid`是用于显示表格数据的一种控件,在.NET Framework中,`...

    easyui datagrid 增加鼠标悬停弹窗事件

    - **监听事件**:首先,我们需要为Datagrid的每一行添加`mouseover`和`mouseout`事件监听器。 - **获取行数据**:在`mouseover`事件触发时,我们可以获取当前行的数据,这通常通过`$(this).data()`或`$(this)....

    Datagrid键盘事件响应与水晶报表使用总结

    由于单元格的焦点处理,直接在Datagrid的事件处理程序中可能无法捕捉到这些事件。在.NET环境中,Datagrid的每个单元格由DataGridTableStyle和DataGridColumnStyle控制,但VS.NET的代码编辑器中未直接提供对应的类名...

    扩展 jQuery EasyUI Datagrid 数据行鼠标悬停离开事件完整版Demo下载

    jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) Demo 扩展 jQuery EasyUI Datagrid 数据行鼠标悬停离开事件,源码奉献!!!

    easyUI datagrid 实现行上移,下移,置顶,置底,排序提交后台绑定键盘事件

    这通常需要在datagrid中添加事件监听器,捕捉到这些按键事件,然后通过调用EasyUI提供的API,比如`moveUp`和`moveDown`方法,来改变行的显示位置。同时,需要确保相应的数据源(可能是服务器端的数据)也更新以保持...

    datagrid单元格上点击弹出窗口(官网例子)

    2. **DataGridWithPopUpEditor.as** - 这个文件可能是主datagrid组件的类,其中包含了对自定义编辑器的引用和配置,比如设置哪一列使用这个弹出窗口编辑器,以及相关的事件处理逻辑。 3. **PopUpEditor.mxml** - 这...

    flex datagrid doubleclick 实例

    在标题“flex datagrid doubleclick 实例”中,我们关注的是如何实现DataGrid组件对用户双击事件的响应。双击事件通常用于执行更复杂的操作,例如编辑选定的记录或触发一个新的视图。以下将详细介绍这一功能的实现...

    DataGrid中嵌套DataGrid

    3. 进行事件处理:主DataGrid的ItemDataBound事件是一个关键的事件,可以在其中获取到内部DataGrid需要的数据,并对其进行绑定。 4. 处理分页和排序:嵌套的DataGrid可能需要独立的分页和排序功能,这需要在事件处理...

Global site tag (gtag.js) - Google Analytics