Hi PRA,
Check this example. Now please take its reference and correct your code.
Form Design
Namespaces
using System.Data.SqlClient;
using System.Data;
Code
C#
Form1.cs
public Form1()
{
InitializeComponent();
selectClass.getTaxables();
selectClass.selectProduct(dataGridView1);
}
clsSelect selectClass = new clsSelect();
decimal taxPercsent = 0, vatPercsent = 0;
private void formOrder_Load(object sender, EventArgs e)
{
selectClass.selectProduct(dataGridView1);
}
private double subtotal()
{
int i = 0;
int j = 0;
double k = 0;
i = 0;
j = 0;
k = 0;
try
{
j = listView1.Items.Count;
for (i = 0; i <= j - 1; i++)
{
k = k + Convert.ToDouble(listView1.Items[i].SubItems[3].Text);
}
return k;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return k;
}
private void button1_Click(object sender, EventArgs e)
{
selectClass.getTaxables();
taxPercsent = selectClass.tax1;
vatPercsent = selectClass.tax2;
try
{
if (listView1.Items.Count > 0)
{
//remove the item selected
listView1.FocusedItem.Remove();
//Reinitialize prices breakdown
for (int col = 0; col <= listView1.Items.Count - 1; col++)
{
selectClass.calcTaxAmt(decimal.Parse(taxPercsent.ToString()), decimal.Parse(vatPercsent.ToString()), decimal.Parse("0.00"), decimal.Parse(lblTotalAmt.Text));
lblTaxAmt.Text = selectClass.tax1_Amt.ToString();
lblVat.Text = selectClass.tax2_Amt.ToString();
lblSubTotal.Text = selectClass.netPrice.ToString();
lblTotalAmt.Text = subtotal().ToString();
}
}
else if (listView1.Items.Count <= 0)
{
listView1.Items.Clear();
lblTaxAmt.Text = "Tax Amount 0.00";
lblVat.Text = "VAT 0.00";
lblSubTotal.Text = "SubTotal 0.00";
lblTotalAmt.Text = "0.00";
lblTotalAmt.Text = "0.00";
}
}
//do nothing
catch (Exception ex)
{
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
for (int col = 0; col <= listView1.Items.Count - 1; col++)
{
//listView1.Items[col].SubItems[1].Text = (Convert.ToInt32(listView1.Items[col].SubItems[1].Text).ToString()) ;
//listView1.Items[col].SubItems[3].Text = (Convert.ToInt32(listView1.Items[col].SubItems[3].Text).ToString());
selectClass.calcTaxAmt(decimal.Parse(taxPercsent.ToString()), decimal.Parse(vatPercsent.ToString()), decimal.Parse("0.00"), decimal.Parse(lblTotalAmt.Text));
lblTaxAmt.Text = selectClass.tax1_Amt.ToString();
lblVat.Text = selectClass.tax2_Amt.ToString();
lblSubTotal.Text = selectClass.netPrice.ToString();
lblTotalAmt.Text = subtotal().ToString();
}
if (listView1.Items.Count >= 0)
{
// btnModify.Enabled = true;
// btnComplimentary.Enabled = true;
}
else
{
// btnModify.Enabled = false;
// btnComplimentary.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Throwing Exception - Fronty", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
string proNam = row.Cells[0].Value.ToString();
//CEHECKING STATUES
try
{
ListViewItem listVItem = new ListViewItem();
listVItem.SubItems.Add("1");
listVItem.SubItems.Add(row.Cells[0].Value.ToString());
listVItem.SubItems.Add(row.Cells[1].Value.ToString());
listView1.Items.Add(listVItem);
for (int col = 0; col <= listView1.Items.Count - 1; col++)
{
selectClass.calcTaxAmt(decimal.Parse(taxPercsent.ToString()), decimal.Parse(vatPercsent.ToString()), decimal.Parse("0.00"), decimal.Parse(lblTotalAmt.Text));
lblTaxAmt.Text = "Tax Amount " + selectClass.tax1_Amt.ToString();
lblVat.Text = "VAT "+selectClass.tax2_Amt.ToString();
lblSubTotal.Text = "SubTotal" + selectClass.netPrice.ToString();
}
lblTotalAmt.Text = "Total "+subtotal().ToString();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Throwing Exception - Fronty", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
clsSelect.cs
class clsSelect
{
public string dbPath = @"Data Source=WIN-GI6MOMOJ2V8\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
SqlDataReader reader;
public decimal tax1, tax2, tax3;
public decimal tax1_Amt, tax2_Amt, tax3_Amt, netPrice;
//call taxes
public void getTaxables()
{
try
{
SqlConnection con = new SqlConnection(dbPath);
con.Open();
string sql = "select tax_1, tax_2, tax_3 from tblTax";
SqlCommand cmd = new SqlCommand(sql, con);
reader = cmd.ExecuteReader();
if (reader.Read())
{
tax1 = decimal.Parse(reader[0].ToString());
tax2 = decimal.Parse(reader[1].ToString());
tax3 = decimal.Parse(reader[2].ToString());
}
else
{
tax1 = 0;
tax2 = 0;
tax3 = 0;
}
reader.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void calcTaxAmt(decimal tax1, decimal tax2, decimal tax3, decimal totalAmt)
{
try
{
tax1_Amt = (tax1 / 100) * totalAmt;
tax2_Amt = (tax2 / 100) * totalAmt;
tax3_Amt = (tax3 / 100) * totalAmt;
netPrice = totalAmt - (tax1_Amt + tax2_Amt + tax3_Amt);
}
catch (Exception ex)
{
}
}
//select product from table
public void selectProduct(DataGridView dgv)
{
try
{
SqlConnection con = new SqlConnection(dbPath);
con.Open();
string sql = "select proName,proPrice from tblProducts";
SqlCommand cmd = new SqlCommand(sql, con);
DataSet dsd = new DataSet();
DataTable data = new DataTable();
// SqlDataReader reader = cmd.ExecuteReader();
SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
adapter.Fill(dsd, sql);
dgv.DataSource = dsd;
dgv.DataMember = sql;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Screenshot