Monday, 18 March 2013

Saving Data Using DataGridview and Doing Calculations in it Billing Software & Inventory Software



Introduction  

 Billing and Invoicing Software Included. Calculations in Data   Grid view for billing software and calculating total at the last.

Hi there are many programmers who face the problem into creating Invoicing and Billing Software this will help them to save the data(Items via DataGridview) in database. And this is for those basic programmers who don't know how to save data using DataGridview in Window Forms using C# and SQL SERVER. Download Sample   

Code For Saving Data In Database( MS SQL SERVER ) via DataGridview  

Here i have created a function for collecting the information entered by the use and after that he can save all that information in the database.

        void Save()
        {

            try
            {

                if (dataGridView1.Rows.Count > 1)
                {

                    for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                    {

                        string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();

                        string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();

                        string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();

                        string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();

                        string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();

                        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
                        {

                            string insert = "INSERT INTO InvoiceItems(SNo,Quantity,Rate,Description,Total) VALUES (@SNo,@Quantity,@Rate,@Description,@Total)";

                            con.Open();

                            SqlCommand cmd = new SqlCommand(insert, con);

                            cmd.Parameters.AddWithValue("@SNo", col1.ToString());

                            cmd.Parameters.AddWithValue("Description", col2.ToString());

                            cmd.Parameters.AddWithValue("@Quantity", col3.ToString());

                            cmd.Parameters.AddWithValue("@Rate", col4.ToString());

                            cmd.Parameters.AddWithValue("Total", col5.ToString());

                            cmd.ExecuteNonQuery();

                            con.Close();

                        }

                    }

                }

            }

            catch (Exception ex)
            {

                ex.Message.ToString();

            }

        }

Code For Doing  Calculations In DataGridview 

Code will be done at the CellEndEdit Event of the Datagridview.So when user clicks on next column the value gets calculated. The total net amount also gets calculated automatically at the cellend event of the gridview.Column Index No refers the values which is to be multipled. Here i have multiplied 2nd & 3rd column and the value is achieved in 4th column. 

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs)
        {
            try
            {     
if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
{
                  
double cell1 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[2].Value);
                  
double cell2 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[3].Value);
                  
if (cell1.ToString() != "" && cell2.ToString() !="")
                 
{
                      
dataGridView1.CurrentRow.Cells[4].Value = cell1 * cell2;
                 
}
             
}
              
label17.Visible = true;
              
decimal tot = 0;
      
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)            
{
                  
tot += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
              
}
              
txt_netamoun.Text = tot.ToString();

            }
            catch(Exception ex)
            {    
MessageBox.Show(ex.ToString());

            }
        }



All these thing i learned when i started working on Billing Software.This is all for a beginner.
Thanks Hope you will like this.

1 comment:

Unknown said...

If you want to know more detail knowledge about Billing software functions and its applications visit us.