C# - 自定义 DataSet 的使用

-------------------------------------------------  SellProdectManager.cs  ---------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Prodect.BLL
{
    public class SellProdectManager
    {
        public static DataSet GetSellProdectDataSet() {
            DataSet ds = new DataSet();//新建一个 DataSet
            DataTable sellProdect = new DataTable("SellProdect");//新建一个 DataTable

DataColumn id = new DataColumn("Id", typeof(int));//新建 Id 列
            id.AutoIncrement = true;//设置该列为自赠列
            id.AutoIncrementSeed = 1;//设置该列的起始值
            id.AutoIncrementStep = 1;//设置自赠的步长

DataColumn name = new DataColumn("Name",typeof(String));//新建 Name 列
            name.AllowDBNull = false;//设置该列是否允许为空
            
            DataColumn repertory = new DataColumn("Repertory",typeof(String));
            repertory.AllowDBNull = false;
            
            //往 DataTable(sellProdect) 的列集合填充新建的列
            sellProdect.Columns.AddRange(new DataColumn[] { id, name, repertory });

//创建与 DataTable(sellProdect) 结构相同的行
            DataRow row = sellProdect.NewRow();
            row["Name"] = "电冰箱";
            row["Repertory"] = 21;

//往 DataTable(sellProdect) 的行集合填充新建的行 
            sellProdect.Rows.Add(row);

//往 DataSet(ds) 的表集合填充 DataTable(sellProdect) 的这个新建的表
            ds.Tables.Add(sellProdect);

// DataSet(ds) 更改确认
            ds.AcceptChanges();
            return ds;
        }
    }
}

----------------------------------------------------  Prodect.cs  -------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Prodect.BLL;

namespace Prodect
{
    public partial class Prodect : Form
    {
        public Prodect()
        {
            InitializeComponent();
        }

DataSet _sellProdect = null;
        //窗体加载触发的事件
        private void Prodect_Load(object sender, EventArgs e)
        {
            //获得 DataSet 
            _sellProdect = SellProdectManager.GetSellProdectDataSet();
            //绑定 DataGridView 数据源
            dgvSell.DataSource = _sellProdect.Tables["SellProdect"];
        }
  
        //行状态发生改变触发的事件
        private void dgvSell_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (dgvSell.SelectedRows.Count == 0)
                return;
            //获得当前 DataGridView 所选择的行
            DataGridViewRow row = dgvSell.SelectedRows[0];
            //获取用于填充行的数据绑定对象,然后强制转换成(DataRowView)
            DataRowView rv = row.DataBoundItem as DataRowView;
            txtName.Text = rv["Name"].ToString();
            txtPrice.Text = rv["Price"].ToString();
            txtCount.Text = rv["Repertory"].ToString();
        }
  
        //删除按钮事件
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvSell.SelectedRows.Count == 0)
                return;
            //获得当前 DataGridView 所选择的行
            DataGridViewRow row = dgvSell.SelectedRows[0];
            //获取用于填充行的数据绑定对象,然后强制转换(DataRowView)
            DataRowView rv = row.DataBoundItem as DataRowView;
            //删除选择的该行
            rv.Delete();
        }
  
        //保存修改按钮事件
        private void btnSave_Click(object sender, EventArgs e)
        {
            //DataSet(_sellProdect)  确认更新
            _sellProdect.AcceptChanges();
        }
  
        //撤消修改按钮事件
        private void btnReject_Click(object sender, EventArgs e)
        {
            //DataSet(_sellProdect)  撤消更新
            _sellProdect.RejectChanges();
        }
    }
}

上一篇:关于Navicat导入MySQL数据库遇到的一些问题


下一篇:【原】Java学习笔记004 - 运算符