C#泛型使用小案例

2021-05-04 18:26

阅读:617

标签:删除   collect   分享   auto   price   图书   gen   案例   button   

 技术分享

 

 

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.Generic;
using Models;
namespace linxin
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
this.dgvinfo.AutoGenerateColumns = false;
}
private List list = new List();

private void showBooks()
{
Book objBook1 = new Book();
{
objBook1.Barcode = 10001;
objBook1.BookName="C#面向对象";
objBook1.Author="林新";
objBook1.UnitPrice = 100;
objBook1.PublishDate=Convert.ToDateTime("2015-11-11");

};
Book objBook2 = new Book();
{
objBook2.Barcode = 10002;
objBook2.BookName = "C#面向对象";
objBook2.Author = "小平";
objBook2.UnitPrice = 100;
objBook2.PublishDate = Convert.ToDateTime("2015-11-12");

};
Book objBook3 = new Book();
{
objBook3.Barcode = 10003;
objBook3.BookName = "C#面向对象";
objBook3.Author = "如此;恋你";
objBook3.UnitPrice = 100;
objBook3.PublishDate = Convert.ToDateTime("2015-11-13");

};
list.Add(objBook1);
list.Add(objBook2);
list.Add(objBook3);

this.dgvinfo.DataSource = this.list;
}

//显示图片列表
private void button1_Click(object sender, EventArgs e)
{
showBooks();
}

private void FrmMain_Load(object sender, EventArgs e)
{

}

 

删除功能
private void button4_Click(object sender, EventArgs e)
{
//找到要删除的图书编码
string barCode = this.dgvinfo.CurrentRow.Cells["Barcode"].Value.ToString();
//根据条形码在集合中查询
//Book deleteBook = new Book();
Book deleteBook = null;
//for (int i = 0; i //{
// if (list[i].Barcode.ToString()==barCode)
// {
// deleteBook = list[i];
// break;
// }
//}
deleteBook = (from b in list where b.Barcode.ToString().Equals(barCode) select b).First();


list.Remove(deleteBook);
//重新绑定数据
this.dgvinfo.DataSource = null;
this.dgvinfo.DataSource = this.list;


}

public void like()
{

dgvinfo.Columns[0].HeaderText = "图书条码";
dgvinfo.Columns[1].HeaderText = "图书名称";
dgvinfo.Columns[2].HeaderText = "图书作者";
dgvinfo.Columns[3].HeaderText = "发布时间";
dgvinfo.Columns[4].HeaderText = "图书价格";

}

 

///


/// 添加元素集合
///

///
///
private void button02_Click(object sender, EventArgs e)
{
Book objBook4 = new Book();
{
objBook4.Barcode = 10004;
objBook4.BookName = "C#面向对象";
objBook4.Author = "你";
objBook4.UnitPrice = 100;
objBook4.PublishDate = Convert.ToDateTime("2015-11-12");

};
list.Add(objBook4);
this.dgvinfo.DataSource = null;
this.dgvinfo.DataSource = this.list;
}


//插入对象
private void button3_Click(object sender, EventArgs e)
{
Book objBook5= new Book();
{
objBook5.Barcode = 10005;
objBook5.BookName = "C#面向对象";
objBook5.Author = "你";
objBook5.UnitPrice = 100;
objBook5.PublishDate = Convert.ToDateTime("2015-11-12");

};
list.Insert(3, objBook5);
this.dgvinfo.DataSource = null;
this.dgvinfo.DataSource = this.list;
}

 

 

class   Book{

private int  Barcode  {get;set};
private string BookName  {get;set};
private string  Author  {get;set};
private doubleUnitPrice {get;set};
private Date PublishDate {get;set};

}

 

    作者 : 林新

 

}
}

C#泛型使用小案例

标签:删除   collect   分享   auto   price   图书   gen   案例   button   

原文地址:http://www.cnblogs.com/linnew/p/7700384.html


评论


亲,登录后才可以留言!