使用datagridview中的值绘制线条 [英] Drawing lines by using values from a datagridview
问题描述
我有一个Windows应用程序。 [ ^ ]
当我点击Calculate时,我会使用这些文本框进行一些计算。
通过这些计算,我获得了一些值,并将它们写在datagridview [ ^ ]
此时,我需要画画一些行使用这些值基本上是我的点的坐标..
有些人告诉我创建一个图片框并创建一个绘画事件。
因此我尝试了这个;
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);
到
Point point1 = new Point(Convert.ToInt32(dataGridViewCrestGeo.Rows[0].Cells[0].Value), Convert.ToInt32(dataGridViewCrestGeo.Rows[0].Cells[0].Value));
我收到一条错误消息,如 [ ^ ]
I当我点击绘图按钮时,需要找到一种绘制这些线条的方法。
我是土木工程师,所有这些对我来说都很复杂。我将不胜感激任何帮助。谢谢。
我的尝试:
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();
}
推荐答案
查看错误消息 - 它不是在抱怨抽奖,而是关于数据的格式你正在尝试转换为数字。这就是FormatException的用途!
最有可能的是,特定单元格中的值不是你想象的那样:它不是整数!
因此将值提取到变量中:
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:
object data = dataGridViewCrestGeo.Rows[0].Cells[0].Value;
在行上放置一个断点,并在调试器中运行您的代码。当它到达那里时,它会停止。单步行,并准确查看数据
中的内容 - 它会告诉您正在查看的数据类型,这应该告诉您为何无法转换为整数。
我们不能为您做到这一点 - 我们无法访问您的DataGridView或其包含的数据!
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!
好的,我设法以某种方式划清界限。我将离开我的解决方案以防其他人需要它。
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();
}
这篇关于使用datagridview中的值绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!