开发者

How do I add an item to a ListView?

I'm currently developing a POS system(windows application) in C# with SQL Server as the database. In my database, I have a product table that contains the following fields: barcode(primary key) , productID,productName ,productQty ,productSize, and productUnitPrice.

In my GUI, I have a TEXTBOX for the barcode, a numericUpDown for the quantity,and a LISTVIEW(7 columns : barcode, ID, Description, Quantity, Size, Regular Price, Sale Price) for the cart.

My problem is, how do I add the product information(barcode, productID, productName, productQty, productSize, productUnitPrice) inside the LISTVIEW based on the 开发者_JS百科barcode that was entered on the barcode TEXTBOX?

//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.
    }
}


This should get you started:

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


Basically, set your listview's itemssource to an empty list, and then fill that list with whatever items have THAT barcode, hook up the textchanged event of the textbox to update that list (Watch out for performance hits tho, use LINQ to objects instead of re-querying the DB)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜