Capture the double-click event of the DataGrid (from a Bao column)

Source: Internet
Author: User
<! -- Startfragment --> 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 ()
{
// Create a dataset with 2 tables and 1 and Relation
Makedataset ();
// Data Binding
Datagrid1.setdatabinding (mydataset, "MERs ");

// Add a style
Addcustomdatatablestyle ();
}

Private void makedataset ()
{
// Create a dataset.
Mydataset = new dataset ("mydataset ");

// Create two Ables.
Datatable tcust = new datatable ("MERs ");

// 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 tables to dataset.
Mydataset. Tables. Add (tcust );


/* Calculate the tables. Create the datarow variable for each customer */
Datarow newrow1;

// Add records to the MERs table.
For (INT I = 1; I <4; I ++)
{
Newrow1 = tcust. newrow ();
Newrow1 ["custid"] = (100 * I). tostring ();
Tcust. Rows. Add (newrow1 );
}

Tcust. Rows [0] ["custname"] = "[wonderful world of Meng xianhui ]";
Tcust. Rows [1] ["custname"] = "net_lover ";
Tcust. Rows [2] ["custname"] = "http://xml.sz.luohuedu.net /";

Tcust. Rows [0] ["custcity"] = "Beijing ";
Tcust. Rows [1] ["custcity"] = "Shanghai ";
Tcust. Rows [2] ["custcity"] = "Henan ";
}

Private void addcustomdatatablestyle ()
{
Datagridtablestyle ts1 = new datagridtablestyle ();
Ts1.mappingname = "customers ";
// Set attributes
Ts1.alternatingbackcolor = color. lightgray;

// Add a textbox Column Style to capture mouse events
Maid ();
Textcol. mappingname = "custid ";
Textcol. headertext = "no ";
Textcol. width = 100;

// Add an event Processor
Textcol. textbox. mousedown + = new mouseeventhandler (textboxmousedownhandler );
Textcol. textbox. DoubleClick + = new eventhandler (textboxdoubleclickhandler );
Ts1.gridcolumnstyles. Add (textcol );

Textcol = new maid ();
Textcol. mappingname = "custname ";
Textcol. headertext = "name ";
Textcol. width = 100;
// Add an event Processor
Textcol. textbox. mousedown + = new mouseeventhandler (textboxmousedownhandler );
Textcol. textbox. DoubleClick + = new eventhandler (textboxdoubleclickhandler );
Ts1.gridcolumnstyles. Add (textcol );

Textcol = new maid ();
Textcol. mappingname = "custcity ";
Textcol. headertext = "Address ";
Textcol. width = 100;
// Add an 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. Maid = 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. Maid = false;
This. datagrid1.datamember = "";
This. datagrid1.headerforecolor = system. Drawing. systemcolors. controltext;
This. Maid = new system. Drawing. Point (11, 9 );
This. Maid = "maid ";
This. datagrid1.size = new system. Drawing. Size (368,144 );
This. Maid = 0;
This. Maid + = new system. Windows. Forms. mouseeventhandler (this. Maid 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 = "double-click event example ";
(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 ("double-click event occurs. Double-click the value: "+ (textbox) sender). Text. tostring ());
}

Private void textboxmousedownhandler (Object sender, mouseeventargs E)
{
If (datetime. Now <gridmousedowntime. addmilliseconds (systeminformation. doubleclicktime ))
{
MessageBox. Show ("double-click event occurs. Double-click the value: "+ (textbox) sender). Text. tostring ());
}
Label1.text = "textbox: click it. ";
}

Private void datagrid=mousedown (Object sender, system. Windows. Forms. mouseeventargs E)
{
Gridmousedowntime = datetime. now;
Label1.text = "datagrid1 press the mouse. ";
}

Private void form1_click (Object sender, system. eventargs E)
{
Label1.text = "";
}
Private void labelease click (Object sender, system. eventargs E)
{
Label1.text = "";
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.