Using system.drawing; using system.componentmodel; using system.windows.form; using system.data ;. using system.data ;. USING SYSTEM.DATA;
Namespace DataGriddoubleClick {using system; using system.collections; using system.componentmodel; using system.windows.form; 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 () {INITIALIZECMOMPONENT (); gridMousedowntime = DATETIME.NOW; setup ();
Private void setup () {// creates a DataSet MakeDataSet () with 2 Table and 1 and Relation DataGrid1.SetDataBinding (MyDataSet, "Customers");
// Add a style addCustomDataTableStyle ();
Private void makeDataSet () {// creates a DataSet. mydataset = new dataset ("MyDataSet"); // Create 2 DataTables. DataTable Tcust = New DataTable ("Customers"); // Create two columns and added to the first a 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.column.add (ccurrent);
// Add tables to DataSet. MyDataSet.Tables.Add (TCUST); / * Calculate Tables. For each customer, create a DataRow variable * / DATAROW newRow1; // Add record to Customers Table. For (int i = 1; I <4; i ) {newrow1 = tcust.newrow (); newrow1 ["custom"] = (100 * i) .tostring (); tcust.rows.add (newrow1);
TCUST.ROWS [0] ["custname"] = "Wonderful World of Mengxian]"; tcust.rows [1] ["CustName"] = "Net_lover"; tcust.rows [2] ["CustName"] = "http://xml.sz.luohuedu.net/";tcust.rows[0]["custcity"] = "Beijing"; tcust.rows [1] ["custom"] = "Shanghai"; TCUST. Rows [2] ["CustCITY"] = "Henan";
Private void addcustomDataTyleStyle () {dataGridTableStyle Ts1 = new dataGridtableStyle (); ts1.mappingname = "customers"; // setting attribute TS1.ALTERNATINGCKCOLOR = Color.lightgray;
// add Textbox column styles, so that we can capture mouse events DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custID"; TextCol.HeaderText = "number"; TextCol.Width = 100;
// add event handlers TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol.TextBox.DoubleClick = new EventHandler (TextBoxDoubleClickHandler); ts1.GridColumnStyles.Add (TextCol);
TextCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custName"; TextCol.HeaderText = "Name"; TextCol.Width = 100; // add event handlers TextCol.TextBox.MouseDown = new MouseEventHandler (TextBoxMouseDownHandler); TextCol. TextBox.doubleClick = New EventHandler (TEXTBOXDOUBLICKHANDLER); Ts1.GridColumnStyles.Add (Textcol);
TextCol = new DataGridTextBoxColumn (); TextCol.MappingName = "custCity"; TextCol.HeaderText = "address"; TextCol.Width = 100; // add event processor 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 (13, 10 ); 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); // // label1 // this.label1.location = new system.drawing.point (5, 179); this.label1. Name = "label1"; this.label1.size = new system.drawing.size (459, 25); 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 (6, 14); this.clientsize = new system.drawing.size (464, 216 This.controls.add (this.label1); this.Controls.add (this.DataGrid1); this.name = "form1"; this.text = "Double-click Event"; (System.comPonentModel. " ISupportinitialize) (THISDATAGRID1))). Endinit (); this.ResumeLayout (false);} #ENDREGION
[Stathread] static void main () {Application.run (new form1 ());} Private Void TextBoxDoubleClickHandler (Object Sender, Eventargs E) {MessageBox.show ("Double-click Event. The value of the mouse is double-hit:" ((((((((( Textbox) sender .Text.toString ());
private void TextBoxMouseDownHandler (object sender, MouseEventArgs e) {if (DateTime.Now Private void DataGrid1_mousedown (Object sender, system.windows) {gridmousedowntime = datetime.now; label1.text = "DataGrid1 mouse presses."; Private void form1_click (object sender, system.eventargs e) {label1.text = "";} private void label1_click (object sender, system.eventargs e) {label1.text = ""}}}