如何将项目添加到ListView?

时间:2010-12-03 03:19:23

标签: c# winforms listview

我目前正在使用SQL Server作为数据库在C#中开发POS系统(Windows应用程序)。在我的数据库中,我有一个产品表,其中包含以下字段:条形码(主键),productID,productName,productQty,productSize和productUnitPrice。

在我的GUI中,我有条形码的TEXTBOX,数量的numericUpDown和购物车的LISTVIEW(7列:条形码,ID,描述,数量,尺寸,常规价格,销售价格)。

我的问题是,如何根据条形码TEXTBOX上输入的条形码在LISTVIEW中添加产品信息(条形码,productID,productName,productQty,productSize,productUnitPrice)?

//Inventory Base Class
public abstract class inventoryBaseClass
{
    public inventoryBaseClass()
    {

    }

    public inventoryBaseClass(uint _id)
    {
        Id = _id;
    }

    public void OpenSqlConn()
    {
        try
        {
            sqlConnection = @"Data Source=PC10\SQLEXPRESS;Initial Catalog=POSDB;Integrated Security=True";
            sqlConn = new SqlConnection(sqlConnection);
            sqlConn.Open();
        }

        catch (Exception ex)
        {
            DialogResult r = MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (r == DialogResult.OK)
                Application.Exit();
        }
    }
}


//Point of Sales Class
public class pointOfSalesClass : inventoryBaseClass
{

    public pointOfSalesClass()
    {

    }

    public pointOfSalesClass(uint _id)
        : base(_id)
    {
        OpenSqlConn();
        string sql = @"Select Barcode, ProductID, ProductName, TotalStocks,Size, Price, SaleAmount FROM PRODUCT WHERE Barcode = +" + _id;
        SqlCmd = new SqlCommand();
        SqlCmd.CommandText = sql;
        SqlCmd.Connection = SqlConn;

    }

}


//Point of sales Form
public partial class Point_of_Sales : Form
{

    //these variables will hold the values that will be retreived in the SELECT statement in the Point of Sales Class
    uint barcode = 0;
    string id = "";
    string productName = "";
    uint qty = 0;
    string size = "";
    double regularPrice = 0.0;
    double salePrice = 0.0;

    //ADD to cart(Listview) Button
    private void AddItem_Click(object sender, EventArgs e)
    {

        //the user enters the barcode on the txtBarcode textbox and the quantity to be purchased on the numericUpDown control
        //When this button is pressed, the select statement will be executed

        pointOfSalesClass addToCart = new pointOfSalesClass(uint.Parse(txtBarcode.Text.ToString()));
        addToCart.SqlDataRdr = addToCart.SqlCmd.ExecuteReader();

        uint quantity = Convert.ToUInt16(numericQty.Value);

        while (addToCart.SqlDataRdr.Read())
        {
            //These are the values to be retreived
            barcode = Convert.ToUInt32(addToCart.SqlDataRdr["Barcode"].ToString());
            id = addToCart.SqlDataRdr["ProductID"].ToString();
            productName = addToCart.SqlDataRdr["ProductName"].ToString();
            qty = Convert.ToUInt32(addToCart.SqlDataRdr["TotalStocks"].ToString());
            size = addToCart.SqlDataRdr["Size"].ToString();
            regularPrice = Convert.ToDouble(addToCart.SqlDataRdr["Price"].ToString());
            salePrice = Convert.ToDouble(addToCart.SqlDataRdr["SaleAmount"].ToString());
        }

        //After retreiving all values in the select statement
        //How do I insert the values(barcode, id, productname, quantity(from the numericUpDown control), size, regularPrice,salePrice) inside the LISTVIEW.
    }
}

2 个答案:

答案 0 :(得分:1)

这应该让你开始:

ListViewItem item = new ListViewItem(
    new string[] { barcode.ToString(), id, productName /* etc */ });
listView1.Items.Add(item);

答案 1 :(得分:0)

基本上,将listview的itemssource设置为空列表,然后用任何具有该条形码的项填充该列表,连接文本框的textchanged事件以更新该列表(注意性能命中,使用LINQ对象而不是重新查询数据库)