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

标签 c# graphics datagridview cell

我正在尝试使用 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/

相关文章:

c# - 在 Linq 查询中尝试

c# - 为什么我的 XmlReader.GetAttribute() 没有返回值?

java - 实现基于节点的图形 GUI 的最佳/最简单方法?

algorithm - IDAT PNG block ,第一个字节意外,我哪里错了?

opengl - 现代图形管道有多少帧深?

vb.net - 清除绑定(bind)到DataSet的DataGridView而不丢失 header VB.NET

c# - BindingNavigator 与 DataGridView 的分页功能?

c# - 比较 JSON 结构

c# - c# 一次只打开一个子窗体实例

c# - 修剪 DataGridView 单元格中的空白区域