Summaries and db Reformatting for a DataGridView

1

Probably did not elaborate a correct title, but I will try to explain my detailed question.

I'm working with WindowsForm in VisualBasic15 I need to make a form where I can get data from tb to db . But I do not just want to get the data and play on DataGridView . I would like this data to be formatted:

Ex:

These are the data saved in my db . Customers and Values.

+-----------+------+
|  Cliente  | Val1 | 
+-----------+------+
| 0000000-1 | 500  | 
| 0000000-2 | 650  | 
| 0000000-3 | 700  |  
| 0000000-2 | 320  |  
| 0000000-2 | 200  | 
| 0000000-3 | 580  | 
+-----------+------+

I would like to fetch all this data, however by playing DataGridView as follows:

+-----------+------+
|  Cliente  | Val1 | 
+-----------+------+
| 0000000-1 | 500  | 
| 0000000-2 | 1170 | 
| 0000000-3 | 1280 |  
+-----------+------+

So that I can display the sums of all Val1 for the respective Customer and play the sum result within 1 Cell

    
asked by anonymous 01.02.2017 / 14:58

1 answer

3

Basically:

System.Data.DataTable dt = new System.Data.DataTable(); //DataTable

string strConection = ""; // informe a string de conexao

using (MySql.Data.MySqlClient.MySqlConnection db = 
               new MySql.Data.MySqlClient.MySqlConnection(strConection))
using (MySql.Data.MySqlClient.MySqlCommand command = db.CreateCommand())
{
    db.Open(); // abre a conexão com o banco
    command.CommandText = 
                     "SELECT cliente, sum(valor) as total FROM tbVendas GROUP BY cliente";
    command.CommandType = System.Data.CommandType.Text;

    dt.Load(command.ExecuteReader()); // carrega o DataTable

    db.Close(); //Fecha a conexão.
}

DataGridView1.DataSource = dt; // carrega a grid

It can be done with classes as well, but I believe your doubt is enough.

    
01.02.2017 / 16:40