C # Fast data input using datagridview

Source: Internet
Author: User

Table input is often required for management software. Table input greatly accelerates data input, increases productivity, and, of course, increases software competitiveness. Recently, when I was using C # For a set of CRM, I successfully used the table control datagriin C #2005 to implement the table input function. Now I will share the specific implementation with you:
1. Initialization
(1) create a C # winform application in VS 2005Program: Datagridviewtest
(2) drag a dview control on the form form1: datagridview1
(3) add two columns in datagridview1:
Column1:
Type: maid
Headertext: Time
Datapropertyname: dutytime
Column2:
Type: maid
Headertext: Time
Datapropertyname: dutytime

(4) add two private attributes to the form1 class:
Private datatable m_table; // enter the drop-down data of the combo control.
Private datatable m_datatable; // The datatable bound to the table, that is, the final data entered by the user.

(5) define a struct in the form1 class

Public struct myrowdata
{
Public myrowdata (INT No, string enday, string cnday)
{
No = no;
Enday = enday;
Cnday = cnday;
}
Public int no;
Public String enday;
Public String cnday;
}

(6) Add the following initialization to the load event form1_load (Object sender, eventargs E) of form1Code:

This. Maid = true;
This. Maid = true;
This. Maid = false;
Datacolumn DC1, DC2, DC3
DC1 = new datacolumn ("no", typeof (INT ));
DC2 = new datacolumn ("enday", typeof (string ));
DC3 = new datacolumn ("cnday", typeof (string ));
M_table.columns.clear ();
M_table.columns.addrange (New datacolumn [] {DC1, DC2, DC3 });
Myrowdata [] datas = new myrowdata [7] {
New myrowdata (1, "Monday", "Monday "),
New myrowdata (2, "Tuesday", "Tuesday "),
New myrowdata (3, "Wednesday", "Wednesday "),
New myrowdata (4, "Thursday", "Thursday "),
New myrowdata (5, "Friday", "Friday "),
New myrowdata (6, "Saturday", "Saturday "),
New myrowdata (7, "Sunday", "Sunday ")
};
This. m_table.rows.clear ();
Foreach (myrowdata idata in datas)
{
This. m_table.rows.add (idata. No, idata. enday, idata. cnday );
}


Datacolumn datadc1, datadc2;

Datadc1 = new datacolumn ("dutytime", typeof (INT ));
Datadc2 = new datacolumn ("dutyperson", typeof (string ));
M_datatable.columns.clear ();
M_datatable.columns.addrange (New datacolumn [] {datadc1, datadc2 });
M_datatable.rows.clear ();

Column1.datasource = This. m_table;
Column1.displaymember = "cnday ";
Column1.valuemember = "no ";

This. datagridview1.datasource = m_datatable;

2. Message Processing (CORE)
Protected override bool processcmdkey (Ref system. Windows. Forms. Message MSG, system. Windows. Forms. Keys keydata) // activate the Enter key
{
Int wm_keydown = 256;
Int wm_syskeydown = 260;

Bool isdatagridview1 = false;
If (this. activecontrol = This. datagridview1)
{
Isdatagridview1 = true;
}
Else
{
If (this. activecontrol is idatagridvieweditingcontrol)
{
If (this. activecontrol as idatagridvieweditingcontrol). editingcontroldatagridview = This. datagridview1)
{
Isdatagridview1 = true;
}
}
}

If (isdatagridview1) // whether the object is in the format of datagridview1
{
If (msg. MSG = wm_keydown then msg. MSG = wm_syskeydown)
{
Switch (keydata)
{
Case keys. Delete:
If (this. datagridview1.currentcell! = NULL
& This. datagridview1.currentcell. rowindex <this. m_datatable.rows.count)
{
This. m_datatable.rows [This. datagridview1.currentcell. rowindex]. Delete ();
This. m_datatable.acceptchanges ();
}
Break;
Case keys. Enter:
If (this. datagridview1.currentcell is datagridviewcomboboxcell
Too many this. maid is datagridviewtextboxcell
) // In the input Cell
{
// Sendkeys. Send ("{F4 }");
If (this. activecontrol is system. Windows. Forms. datagridviewcomboboxeditingcontrol
Too many this. activecontrol is system. Windows. Forms. datagridviewtextboxeditingcontrol
)
{
Sendkeys. Send ("{tab }");
}
Else // non-input status, will be transferred to the input status
{
If (this. datagridview1.currentcell is datagridviewtextboxcell) // press f2 to enter the editing status.
{
Sendkeys. Send ("{F2 }");
}
Else // ComboBox, shortcut key F4, pop-up drop-down box
{
Sendkeys. Send ("{F4 }");

}
}
}
Else // in a non-input grid
{
Sendkeys. Send ("{tab }");
}
Return true;
Break;
}
}
}
Return false;
}
 

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.