Update Chart with data from a column in GridView

0



I have in my project a GridView where I manually enter data in the last column (index 7) and a PieChart that should be updated according to this last column, however, however much I can get the data from column I can not pass them to the Chart. Here is the code where I captured the values:

TimeSpan[] permanencia = new TimeSpan[dataGridView1.Rows.Count];
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            permanencia[i] = TimeSpan.Parse(Convert.ToString(dataGridView1.Rows[i].Cells[7].Value));
        }


So far so good, but when I try to pass the data to update my Chart, nothing happens. Here is the code:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            ds = new DataSet(Convert.ToString(permanencia[i]));
            chart1.DataSource = ds;
            chart1.DataBind();
            chart1.Update();
        }


Could anyone help me? I'm a beginner with Charts ...
Note: I am working with WindowsForm and as already mentioned, the data in this column is generated manually ...

On request, it follows the completion code of the last column, UNICA filled in this way ...

// CALCULO DA PERMANÊNCIA E INSERÇÃO DA COLUNA COM OS RESPECTIVOS DADOS
        DateTime ent = new DateTime();
        DateTime sad = new DateTime();
        TimeSpan permanencia = new TimeSpan();
        DataGridViewRow l = dataGridView1.Rows[0];
        DataGridViewCell c = l.Cells[0];
        dataGridView1.Columns.Add("colunaPermanencia", "Permanencia");

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            ent = Convert.ToDateTime(dataGridView1.Rows[i].Cells[4].Value);
            sad = Convert.ToDateTime(dataGridView1.Rows[i].Cells[6].Value);
            permanencia = sad - ent;
            dataGridView1.Rows[i].Cells["colunaPermanencia"].Value = permanencia.ToString();
        }
    
asked by anonymous 26.05.2017 / 02:36

1 answer

0

Considering that you are using DataTable as the data source of your dataGridView, I think there is a problem in adding a column manually to the control, and prefix it with the data because it will not be added to the DataTable.

I suggest you do this calculation directly in the SQL command and the column is already returned to the DataTable. Avoiding all this code in your Form.

Then just use the same DataTable as the DataSource of the Chart, defining which will be the X and Y axis of the Series you add to it.

I made a sample code: (Note the part of the data was simulated)

//Simulação da fonte de dados
DataTable dt = new DataTable();
dt.Columns.Add("curso");
dt.Columns.Add("permanencia");
Random rnd = new Random();
//Insere dados aleatórios na tabela
for (int i = 0; i < 7; i++)
{
    DataRow r1 = dt.NewRow();
    r1["curso"] = "Curso " + i;
    r1["permanencia"] = Math.Round(new TimeSpan(0, rnd.Next(50), rnd.Next(50)).TotalMinutes,2);
    dt.Rows.Add(r1);
}

//Fonte do DataGridView
dataGridView1.DataSource = dt;
//Fonte do Chart
chart1.DataSource = dataGridView1.DataSource;
//A Serie 0 já é adicionada quando você arrasta o controle para o Form
//Essas configurações podem ser feitas na interface visual
chart1.Series[0].XValueMember = "curso"; //Define o eixo X
chart1.Series[0].YValueMembers = "permanencia"; //Defino o eixo Y 


chart1.DataBind();

More information about Chart:

link

link

    
26.05.2017 / 03:50