我的问题是我的代码无效。扫描条形码时,应将项目添加到网格中。
我所做的是当项目被扫描时,它将出现在文本框中,其名称将出现在另一个文本框中,之后如果项目有库存,项目数据将被添加到datagridview。
这是我的代码。
using (var con = new SqlConnection("Data Source=hamo25.ddns.net;Initial Catalog=POS;Persist Security Info=True;User ID=sa;Password=31045"))
using (var cmd = new SqlCommand("SELECT * FROM Products WHERE Item_Code = @BarCode", con))
{
cmd.Parameters.Add(new SqlParameter("@BarCode", code.Text));
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
foreach (DataGridViewRow row in receiptgrid.Rows)
{
//Condition item out of stock
if (Convert.ToInt32(reader.GetInt32(4)) > 1)
{
repeat:
name.AppendText(Environment.NewLine + reader.GetString(1));
string pname = reader.GetString(1);
string price = reader.GetString(2);
bool Found = false;
if (receiptgrid.Rows.Count > 0)
{
if (Convert.ToString(row.Cells[0].Value) == reader.GetString(1) && Convert.ToString(row.Cells[1].Value) == reader.GetString(2))
{
row.Cells[2].Value = Convert.ToString(1 + Convert.ToInt32(row.Cells[2].Value));
Found = true;
}
if (!Found)
{
receiptgrid.Rows.Add(pname, price, 1, price, date.Text);
}
else
{
receiptgrid.Rows.Add(pname, price, 1, price);
}
//Multiplying the qty by the price ==================================================================
row.Cells[receiptgrid.Columns["Total"].Index].Value = Convert.ToDouble(row.Cells[receiptgrid.Columns["Price"].Index].Value) * Convert.ToDouble(row.Cells[receiptgrid.Columns["QTY"].Index].Value);
Thread.Sleep(500);
name.Text = string.Empty;
name.Select(0, 0);
name.ScrollToCaret();
name.Select(0, 0);
name.ScrollToCaret();
name.Text = string.Empty;
}
//contiton if item is out of stock
else if (Convert.ToInt32(reader.GetInt32(4)) == 1)
{
DialogResult result = MessageBox.Show("This item should be out of stock, Continue anyway?", "POS", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
goto repeat;
}
else
{
MessageBox.Show("This Item is out of Stock", "POS", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
}
}
}
//Calculating the total =================================================
double sum = 0;
for (int i = 0; i < receiptgrid.Rows.Count; i++)
{
sum += Convert.ToDouble(receiptgrid.Rows[i].Cells[3].Value);
}
Totaltxt.Text = sum.ToString();
//end
int qty = 0;
for (int i = 0; i < receiptgrid.Rows.Count; i++)
{
qty += Convert.ToInt32(receiptgrid.Rows[i].Cells[2].Value);
}
Totalqty.Text = qty.ToString();
}
谢谢