建立项目 一个datagridview、一个选项卡(3个选项)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
OleDbConnection conn = new OleDbConnection(myConnstring);
OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
OleDbConnection MyConnection;
MyConnection = new OleDbConnection(myConnstring);
MyConnection.Open();
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" )
{
MessageBox.Show("信息输入不完整,请完整输入!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
else
{
string CommandString = "select bukrs ,text ,addr from t001 where bukrs='" + textBox1.Text.ToString().Trim() + "' and text='" + textBox2.Text.ToString().Trim() + "' and addr='" + textBox3.Text.ToString().Trim() + "'";
OleDbCommand cmd = new OleDbCommand(CommandString, MyConnection);
if (cmd.ExecuteScalar() != null)
{
MessageBox.Show("该记录已存在,请修改输入!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
textBox1.Clear();
textBox1.Focus();
MyConnection.Close();
}
else
{
cmd.CommandText = "insert into [t001]([bukrs],[text],[addr])values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
cmd.ExecuteNonQuery();
MessageBox.Show("新数据添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Clear();
textBox1.Focus();
textBox2.Clear();
textBox3.Clear();
}
}
}
//以下代码就是为了刷新记录集保证datagridview数据更新
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb");
OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//MessageBox.Show("您单击的是第" + (e.RowIndex + 1) + "行第" + (e.ColumnIndex + 1) + "列!");
//MessageBox.Show("单元格的内容是:" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
textBox10.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox11.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox12.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
textBox8.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
OleDbConnection MyConnection;
MyConnection = new OleDbConnection(myConnstring);
MyConnection.Open();
string CommandString = "delete from t001 where id= " + Convert.ToInt32(textBox11.Text) + " ";
OleDbCommand cmd = new OleDbCommand(CommandString, MyConnection);
cmd.ExecuteNonQuery();
MyConnection.Close();
MessageBox.Show("成功删除", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb");
OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
dataGridView1.DataSource = ds.Tables[0];
}
private void button3_Click(object sender, EventArgs e)
{
string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb";
OleDbConnection MyConnection;
MyConnection = new OleDbConnection(myConnstring);
MyConnection.Open();
string CommandString = "update [t001] set [bukrs]='" + textBox9.Text + "',[text]='" + textBox8.Text + "',[addr]='" + textBox7.Text + "' where [id]= " + Convert.ToInt32(textBox12.Text) + "";
OleDbCommand cmd = new OleDbCommand(CommandString, MyConnection);
cmd.ExecuteNonQuery();
MyConnection.Close();
MessageBox.Show("修改成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\access.mdb");
OleDbDataAdapter da = new OleDbDataAdapter("select * from t001", conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
dataGridView1.DataSource = ds.Tables[0];
}
}
}