I would like to try plotting x and multiple y data from data residing in DataGridView. I populate datagridview but have no idea how to handle an event to get it plotted. I am using windows forms. I am trying to plot via OnPaint but there is no plot. Here is the relevant part:
Have you stepped through the routine to see what the value of each cell was as you attempt to cast it the value to float? It seems to be having trouble with the casting, but since I can't see what is in each datagrid cell, it's hard to tell which line is throwing that at you and why.
I created a small test of your issue, and this onPaint method is working for me... there are little things I'm not handling such as new rows that are auto-created in the grid as I enter values, etc... but... it draws the lines from point to point as I add values to the grid without exceptions being throw. Hope this helps...
(Note: paintEmpty() is just a method I created so the panel woudl say "Invalid Data" if less than two rows existed...)
private void onPaintPanel(object sender, PaintEventArgs e)
Graphics g = e.Graphics;
if (this.dataGridView1.Rows.Count < 2)
foreach (DataGridViewRow row in this.dataGridView1.Rows)
if ((row.Index + 1) < this.dataGridView1.Rows.Count)
// this point
float x1 = Convert.ToSingle(row.Cells.Value);
float y1 = Convert.ToSingle(row.Cells.Value);
// next point
float x2 = Convert.ToSingle(this.dataGridView1.Rows[row.Index + 1].Cells.Value);
float y2 = Convert.ToSingle(this.dataGridView1.Rows[row.Index + 1].Cells.Value);
// dont plot backwards...
if (x2 >= x1)
g.DrawLine(Pens.Blue, new PointF(x1, y1), new PointF(x2, y2));
One question is: how do I step through routine?
The other, bigger problem is: there is still no draing generated. I created windows forms window with two containers: one datagridview and the other one is chart. Where is the missing connection? I am doing data binding, e.g. I have a script to import values into datagridview table but how do I draw it? What's the trigger or event?
Hmm, well, sounds like you are having a basic problem with understanding exactly what you are working with as far as controls and how they utilize information.
Okay, first, to step through, you simply put a break-point on a line in your code where you want to start following along, then run your program in debug (F5), when the code executing hits a line with a break-point, it will stop there and hilight that line in yellow. While the code is stopped, you can hover over variables to see values, or use the immediate window to query objects or execute one-off lines of code to test things. Use [F10] to step line-by-line from that point... there are other F keys to step over, step into, etc... but you'll just need to study debugging in your IDE.
Finally... if you are trying to use a chart control to plot this data... you are quite a ways off base attempting to do any drawing... after all, that is what the chart control was created to handle. The chart control will have a way to feed it the data points, and it will take care of drawing everything in the format you choose in that control.
Not sure why you're bringing the data into a datagridview before charting it... maybe it's just a requirement of what you're doing... but at any rate, what you want to do is study the chart control that you are using, and find out how to feed it your data points... you shouldn't be concerned with onPaint methods at all for this... (unless you are creating your own chart control, of course...)
You can try this... (if you're using Microsoft Chart Control...)
It looks like it's in VB... but it should give you the general idea of what you're looking to do... you want to fill the chart's "series" with data, and let the chart control handle displaying the chart according to that data.
You don't feed the file, you feed the data... you need to...
1. Get the data from the text file.
2. Store it in a format needed by the chart control.
3. Feed the data to the chart control (usually by setting ChartData, or adding values to a Series, etc...)
If you need help with the individual steps, or having errors reading a file, not sure how to store the values in a list or array, etc etc... you can ask and you will probably get more help on those specific types of issues. It's hard for people to help when it is just sort of a wide open project and we don't know what the requirements are, what type of chart control you are using, etc.
Okay... well, if you have your points (x,y values) from the text file stored... you are merely wanting to set the chart series' data points to those values for your line chart so the control will render it for you. This is a little test I threw together and it seems to work fine... there is probably a better way to get it done, but just to give you something that appears to do what you are describing.
DataTable dtData = new DataTable();
for (double x = 10; x < 101; x += 10)
double y = (double)new Random().Next(100);
foreach (DataRow dr in dtData.Rows)
System.Windows.Forms.DataVisualization.Charting.DataPoint dp = new System.Windows.Forms.DataVisualization.Charting.DataPoint((double)dr, (double)dr);
Right, very useful. So I am trying to read an external data file filled with numbers into a DataTable. It all ends up quite ugly.
First, I can't add the data as x and y (or possibly many y values)
Second, I have problems then constructing datapoint as I am reading data as string via Streamreader
Finally, can;t convert from string format into double
And then probably more problems which I can;t even see
Could you please a couple of tips?
DataTable dtData = new DataTable();