簡體   English   中英

我想更新TextBoxes並在執行更新時添加ListView行嗎?

[英]I want to update TextBoxes and add ListView row while updating what I do?

我需要在更新TextBoxes時在ListView上添加/編輯新行。 我可以做什么? 請給我一些建議。 謝謝。 我嘗試了這段代碼:

private void btnUpdate_Click_1(object sender, EventArgs e)
{
    try
    {
        con = new SqlConnection(cs.DBConn);
        con.Open();
        String cb = "update Invoice_info set  VATPer='" + txtTaxPer.Text + "',VATAmount='" + txttableno.Text + "',DiscountPer='" + txtDiscountPer.Text + "',DiscountAmount='" + txtDiscountAmount.Text + "',GrandTotal= '" + txtTotal.Text + "',TotalPayment= '" + txtTotalPayment.Text + "',PaymentDue= '" + txtPaymentDue.Text + "',Remarks='" + txtCustomerName.Text + "' where Invoiceno= '" + txtInvoiceNo.Text + "'";
        cmd = new SqlCommand(cb);
        cmd.Connection = con;
        cmd.ExecuteReader();
        con.Close();

        //int i = 1;
        //ListView1.BeginUpdate();
        //ListView1.Items.Clear();
        // //var item = new  bool 
        //foreach (var  item in items )
        //{
        //    ListViewItem row = new ListViewItem(i.ToString());
        //    row.SubItems.Add(item.EventId);
        //    row.SubItems.Add(item.Name);
        //    row.SubItems.Add(item.WordPos);
        //    lstXmlItems.Items.Add(row);
        //    i++;
        //}
        //ListView1.EndUpdate();
        //ListView1.Refresh();
        //for (int i = 0; i <= ListView1.Items.Count ; i++)
        for (int i = 0; i < ListView1.Items.Count - 1; i++)
        {
            con = new SqlConnection(cs.DBConn);
            string cd = "insert Into ProductSold(InvoiceNo,ProductID,ProductName,Quantity,Price,TotalAmount) VALUES (@d1,@d2,@d3,@d4,@d5,@d6)";
            cmd = new SqlCommand(cd);
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("d1", txtInvoiceNo.Text);
            cmd.Parameters.AddWithValue("d2", ListView1.Items[i].SubItems[1].Text);
            cmd.Parameters.AddWithValue("d3", ListView1.Items[i].SubItems[2].Text);
            cmd.Parameters.AddWithValue("d4", ListView1.Items[i].SubItems[4].Text);
            cmd.Parameters.AddWithValue("d5", ListView1.Items[i].SubItems[3].Text);
            cmd.Parameters.AddWithValue("d6", ListView1.Items[i].SubItems[5].Text);
            con.Open();
            //  cmd.ExecuteNonQuery ();
            con.Close();
        }

        btnUpdate.Enabled = true;
        MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM