A simple application example of a dview, which only allows you to bind, add, modify, and delete the data of a DataGridView. first look at the Code:
View plaincopy to clipboardprint?
Using System;
Using System. Data;
Using System. Windows. Forms;
Namespace DataGridView
{
Public partial class frmMain: Form
{
Public frmMain ()
{
InitializeComponent ();
}
Private DataSet addressList;
Private void frmMain_Load (object sender, EventArgs e)
{
// Load data from xml
AddressList = new DataSet ("AddressList ");
AddressList. ReadXmlSchema (". \ AddressList. xsd ");
AddressList. ReadXml (". \ AddressList. xml", XmlReadMode. Auto );
AddressList. Locale = System. Globalization. CultureInfo. CurrentUICulture;
AddressList. AcceptChanges ();
// Bind to the datagridview
DataGridView1.AutoGenerateColumns = false;
DataGridView1.DataSource = addressList;
DataGridView1.DataMember = "UserList ";
}
Private void create NToolStripMenuItem_Click (object sender, EventArgs e)
{
Maid = maid [maid. Count-1]. Cells ["UserName"];
DataGridView1.BeginEdit (false );
}
Private void maid (object sender, maid e)
{
If (e. RowIndex! = 1 &&! DataGridView1.Rows [e. RowIndex]. IsNewRow)
{
TsslUserName. Text = maid [e. RowIndex]. Cells ["UserName"]. Value. ToString ();
}
}
Private void exit XToolStripMenuItem_Click (object sender, EventArgs e)
{
This. Close ();
}
Private void maid (object sender, maid e)
{
// Verify the data of the edited Cell
Maid c = maid [e. RowIndex]. Cells [e. ColumnIndex];
Switch (maid [e. ColumnIndex]. Name)
{
Case "UserName ":
Int UserNameMaxLength = addressList. Tables ["UserList"]. Columns ["UserName"]. MaxLength;
If (e. FormattedValue. ToString (). Length> UserNameMaxLength)
{
String UserNameError = "the user name cannot exceed" + UserNameMaxLength. ToString () + "characters ";
C. ErrorText = UserNameError;
E. Cancel = false;
}
Else if (e. FormattedValue. ToString (). Length = 0)
{
String UserNameError = "the user name cannot be blank ";
C. ErrorText = UserNameError;
E. Cancel = false;
}
Else
{
If (c. ErrorText! = String. Empty)
{
C. ErrorText = string. Empty;
}
}
Break;
Case "Telephone ":
Int TelephoneMaxLength = addressList. Tables ["UserList"]. Columns ["UserName"]. MaxLength;
If (e. FormattedValue. ToString (). Length> TelephoneMaxLength)
{
String TelephoneError = "the phone number cannot exceed" + TelephoneMaxLength. ToString () + "characters ";
C. ErrorText = TelephoneError;
E. Cancel = false;
}
Else
{
If (c. ErrorText! = String. Empty)
{
C. ErrorText = string. Empty;
}
}
Break;
Case "QQ ":
Int QQMaxLength = addressList. Tables ["UserList"]. Columns ["QQ"]. MaxLength;
If (e. FormattedValue. ToString (). Length> QQMaxLength)
{
String QQError = "the phone number cannot exceed" + QQMaxLength. ToString () + "characters ";
C. ErrorText = QQError;
E. Cancel = false;
}
Else
{
If (c. ErrorText! = String. Empty)
{
C. ErrorText = string. Empty;
}
}
Break;
Case "MSN ":
Int MSNMaxLength = addressList. Tables ["UserList"]. Columns ["MSN"]. MaxLength;
If (e. FormattedValue. ToString (). Length> MSNMaxLength)
{
String MSNError = "the phone number cannot exceed" + MSNMaxLength. ToString () + "characters ";
C. ErrorText = MSNError;
E. Cancel = false;
}
Else
{
If (c. ErrorText! = String. Empty)
{
C. ErrorText = string. Empty;
}
}
Break;
Case "Address ":
Int AddressMaxLength = addressList. Tables ["UserList"]. Columns ["Address"]. MaxLength;
If (e. FormattedValue. ToString (). Length> AddressMaxLength)
{
String AddressError = "the phone number cannot exceed" + AddressMaxLength. ToString () + "characters ";
C. ErrorText = AddressError;
E. Cancel = false;
}
Else
{
If (c. ErrorText! = String. Empty)
{
C. ErrorText = string. Empty;
}
}
Break;
Case "Remark ":
Int RemarkMaxLength = addressList. Tables ["UserList"]. Columns ["Remark"]. MaxLength;
If (e. FormattedValue. ToString (). Length> RemarkMaxLength)
{
String RemarkError = "the phone number cannot exceed" + RemarkMaxLength. ToString () + "characters ";
C. ErrorText = RemarkError;
E. Cancel = false;