Sums and Reformatting db for a Datagridview

Asked

Viewed 24 times

1

I probably did not draw up a correct title, but I will try to explain my very detailed question.

I’m working with Windowsform at Visualbasic15 I need to make a form where I get data from a tb in a db. But I don’t want to just pick up the dice and play in one DataGridView. I would like this data to be formatted :

Ex:

This is 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 these dice, but playing in one 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 played the sum result within 1 Cell

  • C# or Vb.Net? Do you have any code? if yes put what you have already done?

  • @Virgilionovic I’m using C#. I don’t have code for this formstill, I don’t really know where to start to make this execution.

  • just make a Select with GroupBy and show in Grid. These Values are in how many tables, and if you can enter the table name would be good

  • @Virgilionovic Opa, Values are in only 1 table, call tbVendas where I concentrate all my sales reporting on a column the Client and in another columno Value of sale; each rowa sale.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.