Simple Application Example of WinForm (C #) DataGridView

Source: Internet
Author: User
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;

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.