[英]Drawing Statistics Graph in A Datagridview using C#
我正在嘗試使用c#在datagridview中繪制像圖形一樣的心率。
我有一個10列的datagridview。 第一列和最后兩列用於數據。 我需要將圖形繪制到第二列和第八列之間的6個中間列的單元格中。
我已經設法使用celldisplayrectangle通過使用單元格的右,左,上和下值將圖形繪制到單個單元格中。 Drawcurve方法並使用單元格內的Point工作。 但現在我不知道如何通過使用多個單元格來做到這一點。
所以@ozz。 我付出了一些努力,我創建了一個示例WindowsForms應用程序,然后手動添加了三行。
您必須覆蓋DataGridView函數
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// Your custom graphics goes here
}
我在其中添加了一個小的自定義繪畫,當你填充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);
}
}
這是填充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;
}
//這里是投放屏幕截圖
這是源代碼鏈接: 在此處輸入鏈接描述
唯一不錯的選擇是添加圖像而不是繪制任何圖形,將數據轉換為圖像並將該圖像添加到指定的列中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.