C# datagrideView 控件使用

来源:互联网 发布:台球 知乎 编辑:程序博客网 时间:2024/05/29 17:56
using System;using System.Collections;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 AccessDataBase{    public partial class DBForm : Form    {        private OleDbConnection con = null;        private DataTable dataTable = null;        private DataSet dataSet = null;        private OleDbDataAdapter adapter = null;        private OleDbCommand cmd = null;        public DBForm()        {            InitializeComponent();            con=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;                                            Data Source=f:\\test.mdb");            cmd = con.CreateCommand();            dataTable = new DataTable();            dataSet = new DataSet();            adapter = new OleDbDataAdapter();        }        /// <summary>        /// return  a Datatable to the datagridView         /// </summary>        /// <returns></returns>        public DataTable GetAllData()        {            cmd.CommandText = "select * from person where age >13";            OleDbDataAdapter oda = new OleDbDataAdapter(cmd);            DataTable dt = new DataTable();            oda.Fill(dt);            return dt;        }        public void ShowData()        {            this.dataGridView1.DataSource =  GetAllData();        }        /// <summary>        /// add information into table person        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void butAdd_Click(object sender, EventArgs e)        {            string name = textName.Text;            string age = textAge.Text;            string info = textInfo.Text;            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;                                            Data Source=f:\\test.mdb");            try            {                               OleDbCommand command = con.CreateCommand();                command.CommandText = "insert into person(name,age,info) values('" + name + "','" + age + "','" + info + "')";                con.Open();                command.ExecuteNonQuery();            }            catch (Exception ee)            {                MessageBox.Show(ee.Message);            }            finally            {                con.Close();            }                                         }        private void butRead_Click(object sender, EventArgs e)        {            this.dataGridView1.DataSource = GetAllData();        }        private void GetInformation(out string name,out string age,out string info)        {            name = string.Empty;            age = string.Empty;            info = string.Empty;             OleDbCommand command = new OleDbCommand("Select * from person where age >23",con);            con.Open();            OleDbDataReader dataReader = command.ExecuteReader();                          while (dataReader.Read() )            {                if (dataReader.HasRows)                {                    if (dataReader.Read())                    {                        name = dataReader[0].ToString();                        age = dataReader[1].ToString();                        info = dataReader[2].ToString();                        MessageBox.Show(name + age + info);                    }                }                dataReader.Close();            }//while            dataReader.Close();            con.Close();                     }        /// <summary>        /// 根据主键,删除数据        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void butDelete_Click(object sender, EventArgs e)        {            string varName = string.Empty;            int Age = 0;            string commandText = "delete from person where age=@Age";            cmd = new OleDbCommand(commandText,con);            cmd.Parameters.AddWithValue("@Age",SqlDbType.Int);            con.Open();            foreach(DataGridViewRow dgvRow in dataGridView1.SelectedRows )            {                varName = (string)dgvRow.Cells[0].Value;                Age = int.Parse(dgvRow.Cells[1].Value.ToString());                DialogResult flag= MessageBox.Show( dgvRow.Cells[0].Value.ToString(),"Are you sure ?",MessageBoxButtons.YesNo);                if(flag==DialogResult.Yes)                { cmd.Parameters["@age"].Value=Age;                 dataGridView1.Rows.Remove(dgvRow);                 cmd.ExecuteNonQuery();                }                else if(flag==DialogResult.No)                    MessageBox.Show("删除取消!");            }            con.Close();        }//end of ArrayList               private void butSearch_Click(object sender, EventArgs e)        {        }//method Update    }}

原创粉丝点击