gpt4 book ai didi

c# - 使用 C# 在 Datagridview 中绘制统计图

转载 作者:行者123 更新时间:2023-11-30 14:32:49 25 4
gpt4 key购买 nike

我正在尝试使用 C# 在数据 GridView 中绘制心率图形。

我有一个包含 10 列的数据 GridView 。第一列和最后两列用于数据。我需要将图形绘制到第二列和第八列之间的 6 个中间列的单元格中。

我通过使用单元格的右值、左值、顶部值和底部值,成功地使用 celldisplayrectangle 将图形绘制到单个单元格中。 Drawcurve 方法和在单元格内使用点有效。但现在我不知道如何使用多个单元格来做到这一点。

最佳答案

所以@ozz。我付出了一些努力,创建了一个示例 WindowsForms 应用程序,然后我手动添加了三行。

您必须重写 DataGridView 函数

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// Your custom graphics goes here
}

我在其中添加了一点自定义绘画,当您填充 DataGridView 时,它会调用此 _CellPainting 方法,并且所有绘画都从行开始直到结束。您可以指定可以绘制哪一行或哪个单元格。

下面是自定义绘画的完整函数。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
try
{
if (this.dataGridView1.Columns["image"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);

using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);

// Draw the inset highlight box.
e.Graphics.DrawRectangle(Pens.Blue, newRect);
e.Graphics.DrawEllipse(new Pen(Color.Red), newRect);

// Draw the text content of the cell, ignoring alignment.
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

这是填充 DatGridView 的按钮背后的功能

private void cmdPopulate_Click(object sender, EventArgs e)
{
if (dataGridView1.ColumnCount > 0)
{
dataGridView1 = new DataGridView();
}
DataTable dt = new DataTable();
dt.Columns.Add("number");
dt.Columns.Add("name");
dt.Columns.Add("image");

dt.Rows.Add(new object[] { "Item 1","Apple","" });
dt.Rows.Add(new object[] { "Item 2", "Orange", "" });
dt.Rows.Add(new object[] { "Item 3", "Banana", "" });
dataGridView1.DataSource = dt.DefaultView;
}

//这里是截图形式 enter image description here

这里是源码链接: enter link description here

关于c# - 使用 C# 在 Datagridview 中绘制统计图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17782167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com