Datagridview instance 2

Source: Internet
Author: User

Using system;
Using system. Windows. forms;

Public class form1: Form
{
Private datagridview maid = new datagridview ();

// Declare an arraylist to serve as the data store.
Private system. Collections. arraylist MERs =
New system. Collections. arraylist ();

// Declare a customer object to store data for a row being edited.
Private customer customerinedit;

// Declare a variable to store the index of a row being edited.
// A value of-1 indicates that there is no row currently in edit.
Private int rowinedit =-1;

// Declare a variable to indicate the commit scope.
// Set this value to false to use cell-level commit scope.
Private bool rowscopecommit = true;

[Stathreadattribute ()]
Public static void main ()
{
Application. Run (New form1 ());
}

Public form1 ()
{
// Initialize the form.
This. Maid = dockstyle. Fill;
This. Controls. Add (this. datagridview1 );
This. Load + = new eventhandler (form1_load );
This. Text = "datagridview virtual-mode demo (Row-level commit scope )";
}

Private void form1_load (Object sender, eventargs E)
{
// Enable virtual mode.
This. datagridview1.virtualmode = true;

// Connect the virtual-mode events to event handlers.
This. Maid + = new
Datagridviewcellvalueeventhandler (maid );
This. Maid + = new
Datagridviewcellvalueeventhandler (maid );
This. Maid + = new
Datagridviewroweventhandler (maid newrowneeded );
This. Maid + = new
Datagridviewcelleventhandler (maid rowvalidated );
This. Maid + = new
Questioneventhandler (maid rowdirtystateneeded );
This. Maid + = new
Questioneventhandler (maid cancelrowedit );
This. Maid + = new
Maid (maid userdeletingrow );

// Add columns to the datagridview.
Datagridviewtextboxcolumn companynamecolumn = new
Datagridviewtextboxcolumn ();
Companynamecolumn. headertext = "company name ";
Companynamecolumn. Name = "company name ";
Datagridviewtextboxcolumn contactnamecolumn = new
Datagridviewtextboxcolumn ();
Contactnamecolumn. headertext = "contact name ";
Contactnamecolumn. Name = "contact name ";
This. Maid. Add (companynamecolumn );
This. Maid. Add (contactnamecolumn );
This. Maid =
Datagridviewautosizecolumnsmode. allcells;

// Add some sample entries to the Data Store.
This. Customers. Add (new customer (
"Bon app'", "Laurence lebihan "));
This. Customers. Add (new customer (
"Bottom-dollar markets", "Elizabeth Lincoln "));
This. Customers. Add (new customer (
"B's beverages", "Victoria Ashworth "));

// Set the row count, including the row for new records.
This. Maid = 4;
}

Private void maid (Object sender,
System. Windows. Forms. datagridviewcellvalueeventargs E)
{
// If this is the row for new records, no values are needed.
If (E. rowindex = This. datagridview1.rowcount-1) return;

Customer customertmp = NULL;

// Store a reference to the customer object for the row being painted.
If (E. rowindex = rowinedit)
{
Customertmp = This. customerinedit;
}
Else
{
Customertmp = (customer) This. MERs [E. rowindex];
}

// Set the cell value to paint using the customer object retrieved.
Switch (this. datagridview1.columns [E. columnindex]. Name)
{
Case "company name ":
E. value = customertmp. companyName;
Break;

Case "contact name ":
E. value = customertmp. contactname;
Break;
}
}

Private void maid (Object sender,
System. Windows. Forms. datagridviewcellvalueeventargs E)
{
Customer customertmp = NULL;

// Store a reference to the customer object for the row being edited.
If (E. rowindex <this. MERs. Count)
{
// If the user is editing a new row, create a new customer object.
If (this. customerinedit = NULL)
{
This. customerinedit = new customer (
(Customer) This. MERs [E. rowindex]). companyName,
(Customer) This. MERs [E. rowindex]). contactname );
}
Customertmp = This. customerinedit;
This. rowinedit = E. rowindex;
}
Else
{
Customertmp = This. customerinedit;
}

// Set the appropriate customer property to the cell value entered.
String newvalue = E. value as string;
Switch (this. datagridview1.columns [E. columnindex]. Name)
{
Case "company name ":
Customertmp. companyName = newvalue;
Break;

Case "contact name ":
Customertmp. contactname = newvalue;
Break;
}
}

Private void maid (Object sender,
System. Windows. Forms. datagridviewroweventargs E)
{
// Create a new customer object when the user edits
// The row for new records.
This. customerinedit = new customer ();
This. rowinedit = This. Maid. Rows. Count-1;
}

Private void maid (Object sender,
System. Windows. Forms. datagridviewcelleventargs E)
{
// Save row changes if any were made and release the edited
// Customer object if there is one.
If (E. rowindex> = This. MERs. Count &&
E. rowindex! = This. Maid. Count-1)
{
// Add the new customer object to the Data Store.
This. Customers. Add (this. customerinedit );
This. customerinedit = NULL;
This. rowinedit =-1;
}
Else if (this. customerinedit! = NULL &&
E. rowindex <this. MERs. Count)
{
// Save the modified customer object in the Data Store.
This. Customers [E. rowindex] = This. customerinedit;
This. customerinedit = NULL;
This. rowinedit =-1;
}
Else if (this. datagridview1.containsfocus)
{
This. customerinedit = NULL;
This. rowinedit =-1;
}
}

Private void maid (Object sender,
System. Windows. Forms. questioneventargs E)
{
If (! Rowscopecommit)
{
// In cell-level commit scope, indicate whether the value
// Of the current cell has been modified.
E. Response = This. datagridview1.iscurrentcelldirty;
}
}

Private void maid (Object sender,
System. Windows. Forms. questioneventargs E)
{
If (this. rowinedit = This. datagridview1.rows. Count-2 &&
This. rowinedit = This. MERs. Count)
{
// If the user has canceled the edit of a newly created row,
// Replace the corresponding customer object with a new, empty one.
This. customerinedit = new customer ();
}
Else
{
// If the user has canceled the edit of an existing row,
// Release the corresponding customer object.
This. customerinedit = NULL;
This. rowinedit =-1;
}
}

Private void maid userdeletingrow (Object sender,
System. Windows. Forms. datagridviewrowcanceleventargs E)
{
If (E. Row. index <this. MERs. Count)
{
// If the user has deleted an existing row, remove
// Corresponding customer object from the data store.
This. Customers. removeat (E. Row. Index );
}

If (E. Row. Index = This. rowinedit)
{
// If the user has deleted a newly created row, release
// The corresponding customer object.
This. rowinedit =-1;
This. customerinedit = NULL;
}
}
}

Public Class Customer
{
Private string companynamevalue;
Private string contactnamevalue;

Public customer ()
{
// Leave fields empty.
}

Public customer (string companyName, string contactname)
{
Companynamevalue = companyName;
Contactnamevalue = contactname;
}

Public String companyName
{
Get
{
Return companynamevalue;
}
Set
{
Companynamevalue = value;
}
}

Public String contactname
{
Get
{
Return contactnamevalue;
}
Set
{
Contactnamevalue = value;
}
}
}

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.