ozz*_*ozz 3 c# graphics datagridview cell
我正在尝试使用c#在datagridview中绘制像图形一样的心率.
我有一个10列的datagridview.第一列和最后两列用于数据.我需要将图形绘制到第二列和第八列之间的6个中间列的单元格中.
我已经设法使用celldisplayrectangle通过使用单元格的右,左,上和下值将图形绘制到单个单元格中.Drawcurve方法并使用单元格内的Point工作.但现在我不知道如何通过使用多个单元格来做到这一点.
所以@ozz.我付出了一些努力,我创建了一个示例WindowsForms应用程序,然后手动添加了三行.
您必须覆盖DataGridView函数
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// Your custom graphics goes here
}
Run Code Online (Sandbox Code Playgroud)
我在其中添加了一个小的自定义绘画,当你填充DataGridView时,它将调用这个_CellPainting方法,并且所有绘图都开始直到Row的结尾.您可以指定可以绘制哪个行或哪个单元格.
下面是自定义绘画的完整功能.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是填充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;
}
Run Code Online (Sandbox Code Playgroud)
//这里是投放屏幕截图

这是源代码链接: 在此处输入链接描述