//注意:请使用VS2010打开以下的源代码。
//源代码地址:http://pan.baidu.com/s/1j9WVR
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} OleDbConnection connection;
OleDbDataAdapter command;
DataSet dataSet;
DataTable table; OleDbCommandBuilder builder; private void Form1_Load(object sender, EventArgs e)
{
//增加年龄数据(1~100)
List<string> AgeList = new List<string>();
for (int i = ; i < ; i++)
{
AgeList.Add((i + ).ToString());
}
string [] AgeArray = AgeList.ToArray();
comboBox1.Items.AddRange(AgeArray);
comboBox1.Text = ""; //查找数据库
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Info.mdb;");
command = new OleDbDataAdapter("Select * From Information", connection);
dataSet = new DataSet("Info");
command.Fill(dataSet, "Information"); builder = new OleDbCommandBuilder(command); //显示数据库
table = dataSet.Tables["Information"];
dataGridView1.DataSource = table;
} private void textBox1_TextChanged(object sender, EventArgs e)
{
char[] tempChars = textBox1.Text.Trim().ToArray();
List<char> validChars= new List<char>(); for (int i=;i<tempChars.Length;i++)
{
if(!char.IsNumber(tempChars[i]))
{
tempChars=validChars.ToArray();
textBox1.Text = new string(tempChars);
textBox1.SelectionStart = textBox1.Text.Length;
break;
}
else
{
validChars.Add(tempChars[i]);
}
}
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text.Trim() == "")
{
throw new Exception("身份识别码不能空!");
}
else if(textBox1.Text.Trim().Length<)
{
throw new Exception("身份识别码不能小于6位!");
} //检查是否有身份识别码重复的
for (int i = ; i < table.Rows.Count; i++)
{
if ((string)table.Rows[i]["ID"] == textBox1.Text.Trim())
{
throw new Exception("已经存在" + textBox1.Text.Trim() + ",请勿重复添加!");
}
} //添加操作
DataRow row = table.NewRow();
row["ID"] = textBox1.Text.Trim();
row["Name"] = textBox2.Text.Trim();
row["Age"] = comboBox1.Text;
if (radioButton1.Checked == true)
{
row["Gender"] = "男";
}
else
{
row["Gender"] = "女";
}
table.Rows.Add(row); command.Update(dataSet, "Information");
dataGridView1.DataSource = table;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void button3_Click(object sender, EventArgs e)
{
try
{
//删除操作
if (dataGridView1.CurrentCell == null)
{
throw new Exception("无任何内容可删!");
} if (dataGridView1.CurrentCell.RowIndex != -)
{
table.Rows[dataGridView1.CurrentCell.RowIndex].Delete();
}
else
{
throw new Exception("未在表格内选择任一个单元格!");
} command.Update(dataSet, "Information");
dataGridView1.DataSource = table;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//表格上的内容填至相应的文本框等控件
textBox1.Text =(string) dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["ID"].Value;
textBox2.Text = (string)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Name"].Value;
comboBox1.Text=(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Age"].Value).ToString();
if ((string)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["Gender"].Value == "男")
{
radioButton1.Checked = true;
}
else
{
radioButton2.Checked = true;
}
} private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.SelectAll();
} private void textBox2_MouseClick(object sender, MouseEventArgs e)
{
textBox2.SelectAll();
} }
}
运行结果: