Update Chart with data from a column in Gridview

Asked

Viewed 319 times

0



I have in my project a Gridview where I manually insert data in the last column (index 7) and a Piechart that must be updated according to this last column, however, as much as I can get the data from the column I cannot pass them to the Chart. Follow code where I capture the valuables:

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. Follow 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 someone help me? I’m a beginner with Charts...
Note: I am working with Windowsform and as already said, the data of this column is generated manually...

On request, follow the code to fill in the last column, to UNIQUE 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();
        }
  • For a Chart, you have to enter row data...columns... X,Y,Z axes anyway, multiple data... this command ds = new Dataset(Convert.Tostring(permanence[i])); I don’t think it will even work

  • show how you fill all the gridview

  • Ready. I edited the question with the code

  • I was referring to how you fill in all gridview data, all columns

  • and what data should appear on the Chart ? time of stay and more ?

  • I’m using Mysql connection and pulling the other data from there. Only this column is different

  • Residence time and name of the course

  • uses a Dataset/ Datatable coming from mysql ?

Show 3 more comments

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 controller, and fill it with the data, as 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 to add to it.

I made an example code: (Obs. The data part 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 informations about Chart:

https://msdn.microsoft.com/pt-br/library/system.windows.forms.datavisualization.charting.chart%28v=vs.110%29.aspx? f=255&Mspperror=-2147217396

http://www.macoratti.net/12/11/c_chart1.htm

  • It helped me a lot to solve the problem. Thank you very much..

Browser other questions tagged

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