Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a windows application. [^]

When I click on Calculate, I make some calculations using those textboxes.
with those calculations, I obtain some values and write them on a datagridview [^]

At this point, I need to draw some lines using those values which are basically coordinates of my points..

Some people told me to create a picturebox and create a paint event.
Therefore I tried this;
Pen myPen = new Pen(Color.Black);

        private void pictureBoxDraw_Paint(object sender, PaintEventArgs e)
        {
            myPen.Width = 2;
            if (binary > 0)
            {
                Point point1 = new Point(10, 20);
                Point point2 = new Point(10, 40);
                e.Graphics.DrawLine(myPen, point1, point2);
            }
            else
            {
            }
        }
      
        private void buttonDraw_Click_1(object sender, EventArgs e)
        {
            binary = 1;
            pictureBoxDraw.Invalidate();
            
        }

        private void buttoncleardraw_Click(object sender, EventArgs e)
        {
            binary = 0;
            pictureBoxDraw.Invalidate();
        }

When I change
Point point1 = new Point(10, 20);
to
Point point1 = new Point(Convert.ToInt32(dataGridViewCrestGeo.Rows[0].Cells[0].Value), Convert.ToInt32(dataGridViewCrestGeo.Rows[0].Cells[0].Value));

I obtain an error message like this [^]
I need to find a way to draw these lines when I click on the Draw button.

I am a civil engineer and all these things are really complicated for me. I would appreciate any kind of help. Thanks.

What I have tried:

Pen myPen = new Pen(Color.Black);

        private void pictureBoxDraw_Paint(object sender, PaintEventArgs e)
        {
            myPen.Width = 2;
            if (binary > 0)
            {
                Point point1 = new Point(Convert.ToInt32(dataGridViewCrestGeo.Rows[0].Cells[0].Value), Convert.ToInt32(dataGridViewCrestGeo.Rows[0].Cells[0].Value));
                Point point2 = new Point(10, 40);
                e.Graphics.DrawLine(myPen, point1, point2);
            }
            else
            {
            }
        }
      
        private void buttonDraw_Click_1(object sender, EventArgs e)
        {
            binary = 1;
            pictureBoxDraw.Invalidate();
            
        }

        private void buttoncleardraw_Click(object sender, EventArgs e)
        {
            binary = 0;
            pictureBoxDraw.Invalidate();
        }
Posted
Updated 19-May-17 5:11am
Comments
[no name] 19-May-17 9:04am    
Not sure why this is complicated for you. The error message tells you exactly what the problem is. The Convert.ToInt32 function can't convert the values to integers because the strings do not contain integer values.
Meriç Kıranoğlu 19-May-17 9:11am    
Hm, Is there a way to draw without converting those values to integer? For example;
value of dataGridViewCrestGeo.Rows[0].Cells[0].Value is 4.522267. I can round this value to 4.52. Can I draw it?
F-ES Sitecore 19-May-17 9:52am    
try decimal.Parse(dataGridViewCrestGeo.Rows[0].Cells[0].Value) instead. You have to be careful that you don't have stray spaces, new lines etc and that Value contains literally "4.522267" and nothing else. Once it is a decimal if you need it to be an int you can normally cast it

int x = (int)decimalVar;

That will obviously lose some precision.
[no name] 19-May-17 10:32am    
Points take integer values. 4.52 no matter how you round it isn't an integer.

Look at the error message - it's not complaining about the draw, but about the format of the data you are trying to convert to a number. That's what a FormatException is there for!

Most likely, the value in that specific cell is not what you think it is: it's not an integer!
So extract the value into a variable:
C#
object data = dataGridViewCrestGeo.Rows[0].Cells[0].Value;
Put a breakpoint on the line, and run your code in the debugger. when it gets there, it will stop. Single step the line, and look at exactly what is in data - it will tell you what kind of data you are looking at, and that should tell you why it fails conversion to an integer.
We can't do that for you - we don't have any access to your DataGridView or the data it contains!
 
Share this answer
 
Ok, somehow I managed to draw the lines. I'm leaving my solution here in case of anyone else needs it :)

Pen myPen = new Pen(Color.Black);

        private void pictureBoxDraw_Paint(object sender, PaintEventArgs e)
        {
            myPen.Width = 2;
            if (binary > 0)
            {

                float distance1 = float.Parse(dataGridViewCrestGeo.Rows[0].Cells[0].Value.ToString()) * -10;
                float distance2 = float.Parse(dataGridViewCrestGeo.Rows[1].Cells[0].Value.ToString()) * -10;

                float elevation1 = float.Parse(dataGridViewCrestGeo.Rows[0].Cells[1].Value.ToString()) * -10;
                float elevation2 = float.Parse(dataGridViewCrestGeo.Rows[1].Cells[1].Value.ToString()) * -10;
                

                e.Graphics.DrawLine(myPen, distance1, elevation1, distance2, elevation2);
                
             
            }
            else
            {
            }
        }
      
        private void buttonDraw_Click_1(object sender, EventArgs e)
        {
            binary = 1;
            pictureBoxDraw.Invalidate();
            
        }

        private void buttoncleardraw_Click(object sender, EventArgs e)
        {
            binary = 0;
            pictureBoxDraw.Invalidate();
        }
 
Share this answer
 

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