Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

Is it possible to display the data from a chart in a Datagridview? My scenario is if a user right clicks on a chart they will be given an options to view the data associated with the chart in the form of a datagridview.

What I have tried:

At the moment I can right and it brings up an empty datagriview.
Posted
Updated 2-Aug-16 1:47am
Comments
lukeer 2-Aug-16 7:06am    
Where's the chart from? Do you have access to the data that fills it? Or is it just a jpg?
BEBE2011 2-Aug-16 7:29am    
I have access to the data.

1 solution

Here an idea how you can solve your request. Keep in mind this would be probably not the most efficient and elegant solution. Now it is your Job to bring this together with your context menu :)
C#
private void chartTest_MouseClick(object sender, MouseEventArgs e)
{
    HitTestResult ht = chartTest.HitTest(e.X, e.Y);
    if (ht.Series != null)
    {
        dataGridViewChart.Columns.Clear();
        dataGridViewChart.Columns.Add("ColX", "X");
        dataGridViewChart.Columns.Add("ColY", "Y");
        this.dataGridViewChart.Rows.Clear();

        foreach(var point in ht.Series.Points)
        {
            this.dataGridViewChart.Rows.Add(point.XValue, point.YValues[0]);
        }
    }
}
I hope it helps.

[Edit]
http://www.dotnetperls.com/datatable[^]-->
Caution:

DataGridView has performance problems with manually adding rows. Using DataTable, List and DataSource helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900