4
Reply

Save edited datagridview into SQL Server database C#

Ask a question
Hector Will

Hector Will

13y
4.9k
1
Hello everyone, 

I need a little bit of help, I have edited my datagridview from the dataset, im having trouble in updating the dataset, and i dont know how to save this edited data back to the original database table. Here's my code:
private void MDIParent1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=UNICO; Database = Jefes; Integrated Security= SSPI");
            SqlDataAdapter vj = new SqlDataAdapter("select * from VIEW_JEFE", con);
            SqlCommandBuilder builder = new SqlCommandBuilder(vj);
            con.Open();
            DataSet ds = new DataSet();
            ds.EnforceConstraints = false;
            vj.Fill(ds, "VIEW_JEFE");
            con.Close();
            DataGrid dg = new DataGrid();
            dg.DataSource = ds;
            dg.DataMember = "VIEW_JEFE";
            dataSetJefes.EnforceConstraints = false;
                        // TODO: This line of code loads data into the 'dataSetJefes.VIEW_JEFE' table. You can move, or remove it, as needed.
            this.vIEW_JEFETableAdapter.Fill(this.dataSetJefes.VIEW_JEFE);



        }
       

        private void button2_Click(object sender, EventArgs e)
        {
            
            for (int i = 0; i < dg.Rows.Count - 1; i++)
            {
                
                dg.Rows[i].Cells[35].Value = decimal.Parse(dg.Rows[i].Cells[9].Value.ToString()) / decimal.Parse(dg.Rows[i].Cells[10].Value.ToString()) * 100;
                if (decimal.Parse(dg.Rows[i].Cells[9].Value.ToString()) == 0 | decimal.Parse(dg.Rows[i].Cells[13].Value.ToString()) == 0)
                    dg.Rows[i].Cells[36].Value = 0;
                else
                    dg.Rows[i].Cells[36].Value = (decimal.Parse(dg.Rows[i].Cells[12].Value.ToString()) / decimal.Parse(dg.Rows[i].Cells[9].Value.ToString())) / (decimal.Parse(dg.Rows[i].Cells[13].Value.ToString()) / decimal.Parse(dg.Rows[i].Cells[10].Value.ToString())) * 100;
                dg.Rows[i].Cells[37].Value = ((decimal.Parse(dg.Rows[i].Cells[29].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[33].Value.ToString())) / (decimal.Parse(dg.Rows[i].Cells[9].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[12].Value.ToString()))) / ((decimal.Parse(dg.Rows[i].Cells[30].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[34].Value.ToString())) / (decimal.Parse(dg.Rows[i].Cells[10].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[13].Value.ToString()))) * 100;
                dg.Rows[i].Cells[38].Value = ((decimal.Parse(dg.Rows[i].Cells[9].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[19].Value.ToString())) / (decimal.Parse(dg.Rows[i].Cells[9].Value.ToString()))) / ((decimal.Parse(dg.Rows[i].Cells[11].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[20].Value.ToString())) / (decimal.Parse(dg.Rows[i].Cells[11].Value.ToString()))) * 100;
                decimal ec = 0;
                decimal eb = 0;
                ec = decimal.Parse(dg.Rows[i].Cells[25].Value.ToString()) / (decimal.Parse(dg.Rows[i].Cells[9].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[12].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[15].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[17].Value.ToString()));
                eb = decimal.Parse(dg.Rows[i].Cells[26].Value.ToString()) / (decimal.Parse(dg.Rows[i].Cells[11].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[14].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[16].Value.ToString()) + decimal.Parse(dg.Rows[i].Cells[18].Value.ToString()));
                dg.Rows[i].Cells[39].Value = ec / eb * 100;

            }
            
        }

Answers (4)