数据读取器和DataGridView控件的使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;

namespace 数据读取器和DataGridView控件的使用
{
    public partial class Form1 : Form
    {
        private OleDbConnection conn;
        private OleDbCommand cmd;
        private OleDbDataReader reader;
        public class Item
        {
            public int CategoryID { get; set; }
            public string CategoryName { get; set; }
            public Item(int id,string name)
            {
                CategoryName = name;
                CategoryID = id;
            }
            public override string ToString()
            {
                return this.CategoryID.ToString();
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb";
            this.conn = new OleDbConnection(connectionString);
            try
            {
                conn.Open();
                cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "SELECT 类别ID,类别名称 FROM 类别";
                this.reader = cmd.ExecuteReader();
                ArrayList categories = new ArrayList();
                while (reader.Read())
                    categories.Add(new Item(reader.GetInt32(0), reader.GetString(1)));
                this.comboBoxCategory.DataSource = categories;
                this.comboBoxCategory.DisplayMember = "CategoryName";
                reader.Close();
                this.cmd.CommandText = "SELECT DISTINCTROW 类别.类别名称,产品.产品名称,产品.单位数量,产品.库存量,产品.中止 FROM 类别 INNER JOIN 产品 ON 类别.类别ID=产品.类别ID WHERE(((产品.中止)=No)) ORDER BY 类别.类别名称,产品.产品名称";
                this.reader = this.cmd.ExecuteReader();
                object []data=new object[this.reader.FieldCount];
                if (this.dataGridView1.Columns.Count == 0)
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                        this.dataGridView1.Columns.Add(reader.GetName(i), reader.GetName(i)); //添加字段名称
                }
                this.dataGridView1.Rows.Clear();
                while (reader.Read())           //读取Reader中的行并放入DataGridView
                {
                    reader.GetValues(data);
                    this.dataGridView1.Rows.Add(data);
                }
                reader.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void checkBoxIsFiltered_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.checkBoxIsFiltered.Checked)
                this.Form1_Load(sender, e);
            else
            {
                this.cmd.CommandText = "SELECT DISTINCTROW 类别.类别名称,产品.产品名称,产品.单位数量,产品.库存量,产品.中止 FROM 类别 INNER JOIN 产品 ON 类别.类别ID=产品.类别ID WHERE (((产品.中止)=No AND 产品.类别ID=?)) ORDER BY 类别.类别名称,产品.产品名称";
                //设置comboBox控件中的参数,对应于上面一条查询语句
                this.cmd.Parameters.Clear();         //必须加入此句,才会使参数有效
                this.cmd.Parameters.AddWithValue("类别ID", this.comboBoxCategory.SelectedValue.ToString());
                try
                {
                    this.reader = this.cmd.ExecuteReader();
                    object[] data = new object[reader.FieldCount];
                    this.dataGridView1.Rows.Clear();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            reader.GetValues(data);
                            this.dataGridView1.Rows.Add(data);
                        }
                    }
                    reader.Close();
                }
                catch (OleDbException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
            }
        }

        private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.checkBoxIsFiltered_CheckedChanged(sender, e);
        }
    }
}

你可能感兴趣的:(Visual,C#)