views:

338

answers:

1

I want to bind a table from a database to a DataGridView, but I want to also add one more row with a sum of the values in the columns with indexes 3,4,7,8,9...

How can I do that?

Thanks!

            DataTable table1 = new DataTable();
            double brutoUkupno1 = 0;
            double porezUkupno1 = 0;
            double doprinosUkupno1 = 0;
            double netoUkupno1 = 0;
            double doprinosTeretUkupno1 = 0;
            double topliObrokUkupno1 = 0;
            double regresUkupno1 = 0;

            Connection con = new Connection();
            table1 = con.boundTable(month, Convert.ToInt32(year)); //This is method which returns DataTable

            table1.Rows.Add(null, null, null, null, null, null, null, null, null, null, null, null, null, null);
            table1.Rows.Add(null, null, null, null, null, null, null, null, null, null, null, null, null, null);

            dgv2.Visible = true;
            dgv2.DataSource = table1;    

            for (int i = 0; i < dgv2.RowCount - 2; i++)
            {
                topliObrokUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[7].Value);
                regresUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[8].Value);
                brutoUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[9].Value);
                porezUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[10].Value);
                doprinosUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[11].Value);
                netoUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[12].Value);
                doprinosTeretUkupno1 += Convert.ToDouble(dgv2.Rows[i].Cells[13].Value); //Now I am having problems with this below, putting things above to dgv2 :
            }

            dgv2.Rows[dgv2.Rows.Count - 1].Cells[0].Value = "Ukupno";

            dgv2.Rows[dgv2.Rows.Count - 1].Cells[3].Value = month.ToString();

            dgv2.Rows[dgv2.Rows.Count - 1].Cells[4].Value = year.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[7].Value = topliObrokUkupno1.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[8].Value = regresUkupno1.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[9].Value = brutoUkupno1.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[10].Value = porezUkupno1.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[11].Value = doprinosUkupno1.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[12].Value = netoUkupno1.ToString();
            dgv2.Rows[dgv2.Rows.Count - 1].Cells[13].Value = doprinosTeretUkupno1.ToString();
            dgv2.Rows[dgv2.RowCount - 2].Height = 3;
            dgv2.Rows[dgv2.RowCount - 2].DefaultCellStyle.BackColor = Color.Black;
A: 

You could put a BindingSource in between the DataGridView and the DataTable. e.g.:

DataGridView dgv = new DataGridView();
BindingSource bs = new BindingSource();
DataTable dt = GetMyDataTable();
dgv.DataSource = bs;
bs.DataSource = dt;

Now just iterate through dt to get the values you need for the new row, and then add a new row to dt:

dt.Rows.Add(value1, value2, ..... );

With a BindingSource in between, the results will automatically be displayed in the DataGridView. Using this method, you can iterate through the DataTable instead of DataGridView cell values.

Ken