基本信息
源码名称:dvg控件使用和泛型集合的应用
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2016-11-29
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace dvg控件使用和泛型集合的应用
{
    public partial class frmMain : Form
    {
        private List<Book> listBook = new List<Book>();

        public frmMain()
        {
            InitializeComponent();
            this.dgvBookList.AutoGenerateColumns = false;
            initPublisher();

        }
        private void initPublisher()
        {
            List<Publisher> listPublisher = new List<Publisher>()
            { 
            new Publisher() { PublisherId = 1 ,PublisherName="北大出版社"} ,

            new Publisher() { PublisherId = 2, PublisherName = "清华出版社" }  ,

            new Publisher() { PublisherId = 3, PublisherName = "武汉出版社" } ,

            new Publisher() { PublisherId = 4, PublisherName = "深大出版社" } ,
            
            new Publisher() { PublisherId = 5, PublisherName = "波大出版社"}
             };
            this.cobPublisher.DataSource = listPublisher;
            this.cobPublisher.DisplayMember = "PublisherName";
            this.cobPublisher.ValueMember = "PublisherId";
            this.cobPublisher.SelectedIndex=-1;

        }

        private void ShowBooks()
        {
            Book objbook1 = new Book() { BookId = 1001, BookName = "UMP技能指导", Author = "张老师", PublishDate = Convert.ToDateTime("2016-5-11"), UnitPrice = 25 };
            Book objbook2 = new Book() { BookId = 1002, BookName = "UMP生计培训", Author = "杨老师", PublishDate = Convert.ToDateTime("2016-6-11"), UnitPrice = 28 };
            Book objbook3 = new Book() { BookId = 1003, BookName = "UMP设备调试", Author = "谢老师", PublishDate = Convert.ToDateTime("2016-7-11"), UnitPrice = 35 };
            Book objbook4 = new Book() { BookId = 1004, BookName = "UMP尺寸优化", Author = "罗老师", PublishDate = Convert.ToDateTime("2016-8-11"), UnitPrice = 45 };
            Book objbook5 = new Book() { BookId = 1005, BookName = "UMP FA分析", Author = "李老师", PublishDate = Convert.ToDateTime("2016-9-11"), UnitPrice = 33 };
            listBook.Add(objbook1);
            listBook.Add(objbook2);
            listBook.Add(objbook3);
            listBook.Add(objbook4);
            listBook.Add(objbook5);
            this.dgvBookList.DataSource = null;
            this.dgvBookList.DataSource = listBook;

        }
        private void btnShowBooks_Click(object sender, EventArgs e)
        {
            ShowBooks();
        }

        private void btnAddBook_Click(object sender, EventArgs e)
        {
            Book objbook6 = new Book() { BookId =Convert.ToInt32( this.txtId.Text), BookName = this.txtName.Text, Author = this.txtAuthor.Text, PublishDate = Convert.ToDateTime(this.txtDate.Text), UnitPrice =Convert.ToInt32( this.txtPrice.Text) };
            listBook.Add(objbook6);
            this.dgvBookList.DataSource = null;
            this.dgvBookList.DataSource = listBook;
            
        }

        private void btnInsertBook_Click(object sender, EventArgs e)
        {
            Book objBook7 = new Book() { BookId = 1007, BookName = "隔壁老王炼成记", Author = "David Wang", PublishDate = Convert.ToDateTime("2016-11-11"), UnitPrice = 1111 };
            listBook.Insert(2, objBook7);
            this.dgvBookList.DataSource = null;
            this.dgvBookList.DataSource = listBook;
        }

        private void btnDeleteBook_Click(object sender, EventArgs e)
        {
            Book deleteBook = null;
            string bookId = this.dgvBookList.CurrentRow.Cells["BookId"].Value.ToString();
            deleteBook = (from b in listBook where b.BookId.ToString().Equals(bookId) select b).First<Book>();
            listBook.Remove(deleteBook);
            this.dgvBookList.DataSource = null;
            this.dgvBookList.DataSource = listBook;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("aaa.trx",FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, this.listBook);
            fs.Close();  
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            if (!File.Exists("aaa.trx")) return;
            FileStream fs = new FileStream("aaa.trx", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            this.listBook = (List<Book>)bf.Deserialize(fs);
            fs.Close();
            this.dgvBookList.DataSource = null;
            this.dgvBookList.DataSource = listBook;

        }
    }
}