捕捉DataGrid的双击事件(C#版本)
点击次数:19 次 发布日期:2008-11-26 11:51:08 作者:源代码网
|
源代码网推荐 源代码网推荐 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=""; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
