编程论坛
400万+工程师在用
华为|鸿蒙开发者日
直播报名
400万+工程师在用
华为|鸿蒙开发者日
直播报名

电子发烧友网工程师

17年用户 16436经验值
擅长:可编程逻辑 MEMS/传感技术 测量仪表 模拟技术 控制/MCU RF/无线
私信 关注

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

2009-2-20 10:23

<p><font face="Verdana">namespace DataGridDoubleClick<br/>{<br/>&nbsp;using System;<br/>&nbsp;using System.Drawing;<br/>&nbsp;using System.Collections;<br/>&nbsp;using System.ComponentModel;<br/>&nbsp;using System.Windows.Forms;<br/>&nbsp;using System.Data;</font></p>
<p><font face="Verdana">&nbsp;public class Form1 : System.Windows.Forms.Form<br/>&nbsp;{<br/>&nbsp;&nbsp;private System.Windows.Forms.DataGrid dataGrid1;<br/>&nbsp;&nbsp;private DataSet myDataSet;<br/>&nbsp;&nbsp;DateTime gridMouseDownTime;<br/>&nbsp;&nbsp;&nbsp; private System.Windows.Forms.Label label1;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;private System.ComponentModel.Container components = null;</font></p>
<p><font face="Verdana">&nbsp;&nbsp;public Form1()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;InitializeComponent();<br/>&nbsp;&nbsp;&nbsp;gridMouseDownTime = DateTime.Now;<br/>&nbsp;&nbsp;&nbsp;SetUp();<br/>&nbsp;&nbsp;}</font></p>
<p><font face="Verdana">&nbsp;&nbsp;private void SetUp()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;// 用2个Table和1和Relation创建DataSet<br/>&nbsp;&nbsp;&nbsp;MakeDataSet();<br/>&nbsp;&nbsp;&nbsp;// 数据绑定<br/>&nbsp;&nbsp;&nbsp;dataGrid1.SetDataBinding(myDataSet, "Customers");</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;//添加样式<br/>&nbsp;&nbsp;&nbsp;AddCustomDataTableStyle();<br/>&nbsp;&nbsp;}</font></p>
<p><font face="Verdana">&nbsp;&nbsp;private void MakeDataSet()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;// 创建DataSet.<br/>&nbsp;&nbsp;&nbsp;myDataSet = new DataSet("myDataSet");<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;// 创建2个DataTables.<br/>&nbsp;&nbsp;&nbsp;DataTable tCust = new DataTable("Customers");<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;// 创建两个列,并添加到第一个表<br/>&nbsp;&nbsp;&nbsp;DataColumn cCustID = new DataColumn("custID");<br/>&nbsp;&nbsp;&nbsp;DataColumn cCustName = new DataColumn("custName");<br/>&nbsp;&nbsp;&nbsp;DataColumn cCurrent = new DataColumn("custCity");<br/>&nbsp;&nbsp;&nbsp;tCust.Columns.Add(cCustID);<br/>&nbsp;&nbsp;&nbsp;tCust.Columns.Add(cCustName);<br/>&nbsp;&nbsp;&nbsp;tCust.Columns.Add(cCurrent);</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;// 把tables添加到DataSet.<br/>&nbsp;&nbsp;&nbsp;myDataSet.Tables.Add(tCust);<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;/* 计算tables.对每个客户,创建DataRow变量 */<br/>&nbsp;&nbsp;&nbsp;DataRow newRow1;<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;// 添加记录到 Customers Table.<br/>&nbsp;&nbsp;&nbsp;for(int i = 1; i &lt; 4; i++)<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;newRow1 = tCust.NewRow();<br/>&nbsp;&nbsp;&nbsp;&nbsp;newRow1["custID"] = (100*i).ToString();<br/>&nbsp;&nbsp;&nbsp;&nbsp;tCust.Rows.Add(newRow1);<br/>&nbsp;&nbsp;&nbsp;}</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;tCust.Rows[0]["custName"] = "【孟宪会之精彩世界】";<br/>&nbsp;&nbsp;&nbsp;tCust.Rows[1]["custName"] = "net_lover";<br/>&nbsp;&nbsp;&nbsp;tCust.Rows[2]["custName"] = "http://xml.sz.luohuedu.net/";</font></p><font face="Verdana">
<p><br/>&nbsp;&nbsp;&nbsp;tCust.Rows[0]["custCity"] = "北京";<br/>&nbsp;&nbsp;&nbsp;tCust.Rows[1]["custCity"] = "上海";<br/>&nbsp;&nbsp;&nbsp;tCust.Rows[2]["custCity"] = "河南";<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;private void AddCustomDataTableStyle()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;DataGridTableStyle ts1 = new DataGridTableStyle();<br/>&nbsp;&nbsp;&nbsp;ts1.MappingName = "Customers";<br/>&nbsp;&nbsp;&nbsp;// 设置属性<br/>&nbsp;&nbsp;&nbsp;ts1.AlternatingBackColor = Color.LightGray;</p>
<p>&nbsp;&nbsp;&nbsp;// 添加Textbox列样式,以便我们捕捉鼠标事件<br/>&nbsp;&nbsp;&nbsp;DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();<br/>&nbsp;&nbsp;&nbsp;TextCol.MappingName = "custID";<br/>&nbsp;&nbsp;&nbsp;TextCol.HeaderText = "序号";<br/>&nbsp;&nbsp;&nbsp;TextCol.Width = 100;</p>
<p>&nbsp;&nbsp;&nbsp;//添加事件处理器<br/>&nbsp;&nbsp;&nbsp;TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);<br/>&nbsp;&nbsp;&nbsp;TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);<br/>&nbsp;&nbsp;&nbsp;ts1.GridColumnStyles.Add(TextCol);</p>
<p>&nbsp;&nbsp;&nbsp;TextCol = new DataGridTextBoxColumn();<br/>&nbsp;&nbsp;&nbsp;TextCol.MappingName = "custName";<br/>&nbsp;&nbsp;&nbsp;TextCol.HeaderText = "姓名";<br/>&nbsp;&nbsp;&nbsp;TextCol.Width = 100;<br/>&nbsp;&nbsp;&nbsp;//添加事件处理器<br/>&nbsp;&nbsp;&nbsp;TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);<br/>&nbsp;&nbsp;&nbsp;TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);<br/>&nbsp;&nbsp;&nbsp;ts1.GridColumnStyles.Add(TextCol);</p>
<p>&nbsp;&nbsp;&nbsp;TextCol = new DataGridTextBoxColumn();<br/>&nbsp;&nbsp;&nbsp;TextCol.MappingName = "custCity";<br/>&nbsp;&nbsp;&nbsp;TextCol.HeaderText = "地址";<br/>&nbsp;&nbsp;&nbsp;TextCol.Width = 100;<br/>&nbsp;&nbsp;&nbsp;//添加事件处理器<br/>&nbsp;&nbsp;&nbsp;TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);<br/>&nbsp;&nbsp;&nbsp;TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);<br/>&nbsp;&nbsp;&nbsp;ts1.GridColumnStyles.Add(TextCol);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;dataGrid1.TableStyles.Add(ts1);<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;protected override void Dispose( bool disposing )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;IF( disposing )<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;#region Windows Form Designer generated code<br/>&nbsp;&nbsp;private void InitializeComponent()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1 = new System.Windows.Forms.DataGrid();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1 = new System.Windows.Forms.Label();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.SuspendLayout();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // dataGrid1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Info;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.WindowText;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.CaptionVisible = false;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.DataMember = "";<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.HeadeRForeColor = System.Drawing.SystemColors.ControlText;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.Location = new System.Drawing.Point(11, 9);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.Name = "dataGrid1";<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.Size = new System.Drawing.Size(368, 144);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.TabIndex = 0;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // label1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1.Location = new System.Drawing.Point(4, 166);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1.Name = "label1";<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1.Size = new System.Drawing.Size(383, 23);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1.TabIndex = 1;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1.Click += new System.EventHandler(this.Form1_Click);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Form1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.ClientSize = new System.Drawing.Size(387, 201);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Controls.AddRange(new System.Windows.Forms.Control[] {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.label1,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.dataGrid1});<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Name = "Form1";<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Text = "鼠标双击事件的例子";<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.ResumeLayout(false);</p>
<p>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;#endregion</p>
<p>&nbsp;&nbsp;[STAThread]<br/>&nbsp;&nbsp;static void Main() <br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;private void TextBoxDoubleClickHandler(object sender, EventArgs e)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;MessageBox.Show("双击事件发生。鼠标双击到的值:"+((TextBox)sender).Text.ToString());<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;if(DateTime.Now &lt; gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))<br/>&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp; MessageBox.Show("双击事件发生。鼠标双击到的值:"+((TextBox)sender).Text.ToString());<br/>&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;label1.Text = "TextBox 鼠标按下了。&nbsp; ";<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;gridMouseDownTime = DateTime.Now;<br/>&nbsp;&nbsp;&nbsp;label1.Text = "DataGrid1 鼠标按下了。&nbsp; ";<br/>&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp; private void Form1_Click(object sender, System.EventArgs e)<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label1.Text="";<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp; private void label1_Click(object sender, System.EventArgs e)<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label1.Text="";<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;}<br/>}<br/></font></p>

更多回帖

打开APP