Saving Data Using DataGridview and Doing Calculations

This article will help for saving data (Items via DataGridView) in the database in Window Forms using C# and SQL Server.

Hi. Many programmers have problems creating Invoicing and Billing Software. This article will help them to save the data (Items via DataGridView) in the 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.

Code For Saving Data In Database (Microsoft SQL Server) via DataGridView

In this article 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

This code will execute for the CellEndEdit Event of the DataGridView. So when the user clicks on the next column the value is calculated. The total net amount is also calculated automatically at the cell end event of the DataGridView. Column Index No refers to the values to be multiplied. Here I have multiplied the 2nd & 3rd columns and the value is put in the 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.