如何检查datagridview1中的值是否等于或小于listview1中的值

时间:2015-08-31 16:27:08

标签: c# asp.net listview datagridview ms-access-2010

我在点击datagridview时有一个Datagridview1和一个Listview1,它将数据传输到文本框,之后我输入了一个项目的数量。 按下保存时,它将保存在数据库中(ms Access)。

如果datagridview中的项目小于或等于,则不会出错。但如果我再次点击该项目,它将要求相同的数量。

我想要做的是检查listview1textbox(quantity)中的项目是否显示消息框,如果listview1.subitem + textbox(quantity)小于datagridview1 + public void dgvData() { try { con = new OleDbConnection(cs); con.Open(); String sql = "SELECT StockID,Menu.MenuID,MenuName,Price,sum(Quantity) from Stocks,Menu where Stocks.MenuID=Menu.MenuId group by StockID,MenuName,Price,Menu.MenuID having sum(quantity > 0) order by MenuName"; cmd = new OleDbCommand(sql, con); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); dataGridView3.Rows.Clear(); while (dr.Read() == true) { dataGridView3.Rows.Add(dr[0], dr[1], dr[2], dr[3], dr[4]); } con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } {1}}。

这是我的dataGridView3加载事件

private void dataGridView3_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
       try
       {
          btnAddCart.Enabled = true;
          txtQtyKilo.Enabled = true;
          txtTotalPayment.Enabled = true;
          AcceptButton = btnAddCart;
          ErrorMenuName.Clear();
          DataGridViewRow dr = dataGridView3.SelectedRows[0];
          txtMenuID.Text = dr.Cells[1].Value.ToString();
          txtMenuName.Text = dr.Cells[2].Value.ToString();
          txtUnitPrice.Text = dr.Cells[3].Value.ToString();
          txtOnHand.Text = dr.Cells[4].Value.ToString();
          txtQtyKilo.Focus();
       }
       catch (Exception ex)
       {
          MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

这是dataGridView3 Click Event

private void AddToCart_Click(object sender, EventArgs e)
        {
            dataGridView3.ClearSelection();
            try
            {
                if (txtClientID.Text == "")
                {
                    ErrorClientID.SetError(txtClientID, "Select A Client!");
                    return;
                }
                if (txtMenuName.Text == "")
                {
                    ErrorMenuName.SetError(txtMenuName, "Select Menu!");
                    return;
                }
                if (txtQtyKilo.Text == "")
                {
                    ErrorQtyPerKilo.SetError(txtQtyKilo, "Enter No. of Quantity Per Kilo!");
                    txtQtyKilo.Focus();
                    return;
                }
                int SaleQty = Convert.ToInt32(txtQtyKilo.Text);
                if (SaleQty == 0)
                {
                    ErrorQtyPerKilo.SetError(txtQtyKilo, "Input Must Be Greater Than 0");
                    txtQtyKilo.Focus();
                    return;
                }
                txtQtyKilo.Enabled = false;
                btnSave.Enabled = true;
                if (ListView1.Items.Count == 0)
                {
                    ListViewItem lst = new ListViewItem();
                    lst.SubItems.Add(txtMenuID.Text);
                    lst.SubItems.Add(txtMenuName.Text);
                    lst.SubItems.Add(txtUnitPrice.Text);
                    lst.SubItems.Add(txtQtyKilo.Text);
                    lst.SubItems.Add(txtTotalAmount.Text);
                    ListView1.Items.Add(lst);
                    txtSubTotal.Text = subtot().ToString();
                    txtST.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert.ToDouble(txtTaxPer.Text) / 100)).ToString();
                    txtGrandTotal.Text = (Convert.ToInt32(txtSubTotal.Text) + Convert.ToInt32(txtST.Text)).ToString();
                    int val1 = 0;
                    int val2 = 0;
                    int.TryParse(txtGrandTotal.Text, out val1);
                    int.TryParse(txtTotalPayment.Text, out val2);
                    int I = (val1 - val2);
                    txtPaymentDue.Text = I.ToString();
                    texboxClear();
                    return;
                }

                for (int j = 0; j <= ListView1.Items.Count - 1; j++)
                {
                    if (ListView1.Items[j].SubItems[1].Text == txtMenuID.Text)
                    {
                        ListView1.Items[j].SubItems[1].Text = txtMenuID.Text;
                        ListView1.Items[j].SubItems[2].Text = txtMenuName.Text;
                        ListView1.Items[j].SubItems[3].Text = txtUnitPrice.Text;
                        ListView1.Items[j].SubItems[4].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[4].Text) + Convert.ToInt32(txtQtyKilo.Text)).ToString();
                        ListView1.Items[j].SubItems[5].Text = (Convert.ToInt32(ListView1.Items[j].SubItems[5].Text) + Convert.ToInt32(txtTotalAmount.Text)).ToString();
                        txtSubTotal.Text = subtot().ToString();
                        txtST.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert.ToDouble(txtTaxPer.Text) / 100)).ToString();
                        txtGrandTotal.Text = (Convert.ToInt32(txtSubTotal.Text) + Convert.ToInt32(txtST.Text)).ToString();
                        int val1 = 0;
                        int val2 = 0;
                        int.TryParse(txtGrandTotal.Text, out val1);
                        int.TryParse(txtTotalPayment.Text, out val2);
                        int I = (val1 - val2);
                        txtPaymentDue.Text = I.ToString();
                        texboxClear();
                        return;
                    }
                }
                ListViewItem lst1 = new ListViewItem();
                lst1.SubItems.Add(txtMenuID.Text);
                lst1.SubItems.Add(txtMenuName.Text);
                lst1.SubItems.Add(txtUnitPrice.Text);
                lst1.SubItems.Add(txtQtyKilo.Text);
                lst1.SubItems.Add(txtTotalAmount.Text);
                ListView1.Items.Add(lst1);
                txtSubTotal.Text = subtot().ToString();
                txtST.Text = Convert.ToInt32((Convert.ToInt32(txtSubTotal.Text) * Convert.ToDouble(txtTaxPer.Text) / 100)).ToString();
                txtGrandTotal.Text = (Convert.ToInt32(txtSubTotal.Text) + Convert.ToInt32(txtST.Text)).ToString();
                int val3 = 0;
                int val4 = 0;
                int.TryParse(txtGrandTotal.Text, out val3);
                int.TryParse(txtTotalPayment.Text, out val4);
                int I1 = (val3 - val4);
                txtPaymentDue.Text = I1.ToString();
                texboxClear();
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

最后我的按钮在ListView1上插入数据

 private void txtSaleQty_Validating(object sender, CancelEventArgs e)
        {
            int val1 = 0;
            int val2 = 0;
            int.TryParse(txtOnHand.Text, out val1);
            int.TryParse(txtQtyKilo.Text, out val2);
            if (val2 > val1)
            {
                ErrorQtyPerKilo.SetError(txtQtyKilo, "Select Available Quantity or < Quantity");
                txtQtyKilo.Text = "";
                txtTotalAmount.Text = "";
                txtQtyKilo.Focus();
                return;
            }
            else
            {
                ErrorQtyPerKilo.Clear();
            }
        }

这是我对datagridview的唯一验证。

List<Faculy> faculties = new ArrayList<>();
....
public void addFaculty(Faculty f) {
  faculties.add(f);
 }
 //... etc 

只检查onhand.text上的数量是否小于输入的数量。

2 个答案:

答案 0 :(得分:1)

我们在这里讨论的是获取列的listview值的总和的代码,即Quantity将这些值添加到Textbox值并与来自datagridview的值进行比较价值。

  double totl = 0;



protected void lvtest_DataBound(object sender, ListViewItemEventArgs e)

{

    if (e.Item.ItemType == ListViewItemType.DataItem)

    {

        Label lbltotalquantity= e.Item.FindControl("lblquantity") as Label;

        totl += Convert.ToDouble(lbltotalquantity.Text);

    }

}



protected void lvtest_PreRender(object sender, EventArgs e)

{

    Label lblTot = this.lvProducts.FindControl("lblTotal") as Label;

    lblTot.Text = totl.ToString(); ;

}

编辑:

 // Optional ToDo: pass a int value indicating which "column" to add up
    public decimal GetSubTotal()
    {

        decimal TotalValue = default(decimal);
        decimal tmp = default(decimal);

        // arrays and collections start at index(0) not (1)


        for (int n = 0; n <= ListView1.Items.Count - 1; n++)
        {
            // ToDo: Not all items must have the same number of SubItems
            // should also check SubItems Count >= 1 for each item
            // try to get the value:
            if (decimal.TryParse(ListView1.Items(n).SubItems(1).Text, tmp))
            {
                TotalValue += tmp;
            }
        }

        return TotalValue;
    }

更新了答案

int num1 = 0; int i = 0; int j = 0; int h = 0;
for (int k = 0; k <= ListView1.Items.Count - 1; k++)
{
  i = Convert.ToInt32(ListView1.Items[k].SubItems[4].Text);  
}

int.TryParse(txtQtyKilo.Text, out j); 
num1 = i + j;
textBox11.Text = Convert.ToString(num1);
int.TryParse(textBox11.Text, out h);
if (num1 <= Convert.ToInt32(txtOnHand.Text))
{
  textBox11.Text = "";
}
else
{
  txtQtyKilo.Text = "";
  textBox11.Text = "";
  MessageBox.Show("Must not be greater than the available menu!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

答案 1 :(得分:-1)

这是代码:

int num1 = 0; int i = 0; int j = 0; int h = 0;
for (int k = 0; k <= ListView1.Items.Count - 1; k++)
{
  i = Convert.ToInt32(ListView1.Items[k].SubItems[4].Text);  
}

int.TryParse(txtQtyKilo.Text, out j); 
num1 = i + j;
textBox11.Text = Convert.ToString(num1);
int.TryParse(textBox11.Text, out h);
if (num1 <= Convert.ToInt32(txtOnHand.Text))
{
  textBox11.Text = "";
}
else
{
  txtQtyKilo.Text = "";
  textBox11.Text = "";
  MessageBox.Show("Must not be greater than the available menu!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

仍然发生错误

i = Convert.ToInt32(ListView1.Items[k].SubItems[4].Text);

如果我输入10/100的数量,并在我的下一个输入中输入100/100

MessageBox.Show("Must not be greater than the available menu!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

节目。但如果我在我的第二个数量输入上输入90/100它就可以成功保存。我该怎么做才能防止这种情况发生?