c#中如何将在数据库查询的数据在listview控件中显示出来
2014-11-26
展开全部
封装数据库字段
namespace ShopModel
{
public class Shops
{
string shopname;
double Price;
int number;
int allnumber;
int singlenumber;
public string Shopname
{
get { return shopname; }
set { shopname = value; }
}
public double Price1
{
get { return Price; }
set { Price = value; }
}
public int Number
{
get { return number; }
set { number = value; }
}
public int Allnumber
{
get { return allnumber; }
set { allnumber = value; }
}
public int Singlenumber
{
get { return singlenumber; }
set { singlenumber = value; }
}
}
}
查询所有数据
private static IList<Shops> Getnumber()
{
string sql = "select * from Shop";
List<Shops> list = new List<Shops>();
using(DataTable dt = DBHelper.GetDataSet(sql))
{
foreach(DataRow row in dt.Rows )
{
Shops shop = new Shops();
shop.Shopname =(string)row["SpName"];
shop.Number =(int)row["SpNumber"];
shop.Price1 = Convert.ToDouble( row["SpPrice"]);
shop.Allnumber = (int)row["SpAllnumber"];
shop.Singlenumber = (int)row["Spsinglenumber"];
list.Add(shop);
}
return list;
}
}
写个方法把数据显示到listview
public void GetRecordBytyep()
{
IList<Records> ilist = RecordManger.GetRecodbytype();
foreach(Records rd in ilist)
{
ListViewItem lvitem = new ListViewItem(); //这个 new 必须放在 遍历里面
lvitem.SubItems.Clear();
lvitem.SubItems[0].Text = rd.Recordid.ToString();
lvitem.SubItems.Add(rd.Pcnumber.ToString() + "号");
lvitem.SubItems.Add(rd.Money.ToString() + "元");
lvitem.SubItems.Add( string.Format("{0:yyyy-MM-dd HH:mm}", rd.Pcontime) );
lvitem.SubItems.Add(string.Format("{0:yyyy-MM-dd HH:mm}", rd.Pcdowntime));
listView1.Items.Add(lvitem);
}
}
刚好做了个小程序,复制一下代码给你参考一下,用到了三层架构。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询