问题描述
//实体类ShoppingItempublicclassShoppingItem{privateBooksbooks;publicBooksBooks{get{returnbooks;}set{books=value;}}privateintquantity;//数量publicintQuantity{get{returnquantity;}set{quantity=value;}}//两参构造函数,用于添加新书publicShoppingItem(){}publicShoppingItem(Booksbooks,intquantity){this.books=books;this.quantity=quantity;}}//业务逻辑层List<ShoppingItem>相当于一个购物车的列表。publicclassShoppingManager{privateList<ShoppingItem>shoppingItems=newList<ShoppingItem>();publicList<ShoppingItem>ShoppingItems{get{returnshoppingItems;}set{shoppingItems=value;}}#region点击商品购买后的发生事件publicvoidAddItem(intbookid){if(shoppingItems==null){this.shoppingItems=newList<ShoppingItem>();}//分为购物车当前是否存在该商品boolhadBuy=false;foreach(ShoppingItemiteminthis.shoppingItems){if(item.Books.Id==bookid){hadBuy=true;item.Quantity+=1;//购物车中该商品的数量增加1break;}}if(!hadBuy)//如果购物车中没有该商品,进行添加新商品到购物车中{Booksbooks=newBookService().GetBookById(bookid);this.shoppingItems.Add(newShoppingItem(books,1));}}}//数据操作层publicclassBookService{publicBooksGetBookById(intbookid){SqlHelpersh=newSqlHelper();Booksbooks=newBooks();stringsql="select*fromBookswhereid="+bookid;SqlDataReadersdr=sh.GetDataReader(sql);while(sdr.Read()){books.Id=Convert.ToInt32(sdr[0].ToString());//编号books.Title=sdr[1].ToString();//标题books.Author=sdr[2].ToString();//作者books.Publisherid=Convert.ToInt32(sdr[3].ToString());//出版社IDbooks.Publishdate=Convert.ToDateTime(sdr[4].ToString());//出版时间books.Isbn=sdr[5].ToString();//书号books.Unitprice=Convert.ToDouble(sdr[6].ToString());//单价books.Contentdescription=sdr[7].ToString();//内容描述books.Toc=sdr[8].ToString();//目录books.Categoryid=Convert.ToInt32(sdr[9].ToString());//类别books.Clicks=Convert.ToInt32(sdr[10].ToString());//点击次数}returnbooks;}}//实体类BookspublicclassBooks{privateintid;publicintId{get{returnid;}set{id=value;}}privatestringtitle;publicstringTitle{get{returntitle;}set{title=value;}}privatestringauthor;publicstringAuthor{get{returnauthor;}set{author=value;}}privateintpublisherid;publicintPublisherid{get{returnpublisherid;}set{publisherid=value;}}privateDateTimepublishdate;publicDateTimePublishdate{get{returnpublishdate;}set{publishdate=value;}}privatestringisbn;publicstringIsbn{get{returnisbn;}set{isbn=value;}}privatedoubleunitprice;publicdoubleUnitprice{get{returnunitprice;}set{unitprice=value;}}privatestringcontentdescription;publicstringContentdescription{get{returncontentdescription;}set{contentdescription=value;}}privatestringtoc;publicstringToc{get{returntoc;}set{toc=value;}}privateintcategoryid;publicintCategoryid{get{returncategoryid;}set{categoryid=value;}}privateintclicks;publicintClicks{get{returnclicks;}set{clicks=value;}}}
解决方案
解决方案二:
这是我某个页面设置一个按钮和DatagridViewprotectedvoidButton1_Click(objectsender,EventArgse){ShoppingManagersm=newShoppingManager();sm.AddItem(4939);List<ShoppingItem>list=newList<ShoppingItem>();list=sm.ShoppingItems;gdvShopping.DataSource=list;gdvShopping.DataBind();}
解决方案三:
前台设置gdvShopping.AutoGenerateColumns=ture;后台:gdvShopping.DataSource=list;gdvShopping.DataBind();
解决方案四:
#2这个都是设置好的,我这边显示出来是这样,商品名我是绑定好了的,就是不能显示出值
解决方案五:
商品名对应哪个字段,没看到ShoppingItem中有商品名