博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WinForm(C#) DataGridView简单应用示例

Posted on 2011-04-06 23:36  moss_tan_jun  阅读(2609)  评论(0编辑  收藏  举报
一个DataGridView简单应用示例,只是简单的实现了DataGridView的数据绑定、添加、修改和删除的操作,先看代码:

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)
{
//从xml中加载数据
addressList = new DataSet("AddressList");
addressList.ReadXmlSchema(".\\AddressList.xsd");
addressList.ReadXml(".\\AddressList.xml", XmlReadMode.Auto);
addressList.Locale = System.Globalization.CultureInfo.CurrentUICulture;
addressList.AcceptChanges();
//绑定到datagridview
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = addressList;
dataGridView1.DataMember = "UserList";
}

private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserName"];
dataGridView1.BeginEdit(false);
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != 1 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
{
tsslUserName.Text = dataGridView1.Rows[e.RowIndex].Cells["UserName"].Value.ToString();
}
}

private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
//验证编辑的单元格的数据
DataGridViewCell c = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
switch (dataGridView1.Columns[e.ColumnIndex].Name)
{
case "UserName":
int UserNameMaxLength = addressList.Tables["UserList"].Columns["UserName"].MaxLength;
if (e.FormattedValue.ToString().Length > UserNameMaxLength)
{
string UserNameError = "用户名不能超过" + UserNameMaxLength.ToString() + "个字符";
c.ErrorText = UserNameError;
e.Cancel = false;
}
else if (e.FormattedValue.ToString().Length == 0)
{
string UserNameError = "用户名不能为空";
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 = "电话号码不能超过" + TelephoneMaxLength.ToString() + "个字符";
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 = "电话号码不能超过" + QQMaxLength.ToString() + "个字符";
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 = "电话号码不能超过" + MSNMaxLength.ToString() + "个字符";
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 = "电话号码不能超过" + AddressMaxLength.ToString() + "个字符";
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 = "电话号码不能超过" + RemarkMaxLength.ToString() + "个字符";
c.ErrorText = RemarkError;
e.Cancel = false;