I am trying to create a program that looks like a LED display. I am using a DataGridView in the windows form using C#. My problem is that I can get the dataGridView to display, but it will not auto-populate the circles in each cell. Below, you will see Dgv_CellPainting method, but I don't know how and where to implement it. Can someone please help? Here is all the code so far...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestDataGrid
{
public partial class Form1 : Form
{
const int cColWidth = 10;
const int cRowHeight = 10;
const int cMaxColumn = 96;
const int cMaxRow = 16;
const int matrixWidth = cColWidth * cMaxColumn + 3;
const int matrixHeight = cRowHeight * cMaxRow + 3;
DataGridView DGV;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DGV = new DataGridView();
DGV.Name = "DGV";
DGV.AllowUserToResizeColumns = false;
DGV.AllowUserToResizeRows = false;
DGV.AllowUserToAddRows = false;
DGV.RowHeadersVisible = false;
DGV.ColumnHeadersVisible = false;
DGV.GridColor = Color.DarkBlue;
DGV.DefaultCellStyle.BackColor = Color.AliceBlue;
DGV.ScrollBars = ScrollBars.None;
DGV.Size = new Size(matrixWidth, matrixHeight);
DGV.Location = new Point(0, 0);
DGV.ForeColor = Color.Transparent;
for (int i = 0; i < cMaxColumn; i++)
{
DataGridViewImageColumn imageCell = new DataGridViewImageColumn();
DGV.Columns.Add(imageCell);
DGV.Columns[i].Name = i.ToString();
DGV.Columns[i].Width = cColWidth;
DGV.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
for (int j = 0; j < cMaxRow; j++)
{
DataGridViewRow row = new DataGridViewRow();
row.Height = cRowHeight;
DGV.Rows.Add(row);
}
Controls.Add(DGV);
}
private void Dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
Brush Brs = new SolidBrush(Color.Blue);
GraphicsExtensions.FillCircle(e.Graphics, Brs, e.CellBounds.Location.X + 5, e.CellBounds.Location.Y + 10, 5);
e.Handled = true;
}
}
}
I also had to make a separate class for the GraphicsExtensions...here is the code...
<pre lang="c#">using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDataGrid
{
public static class GraphicsExtensions
{
public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
}
}
What I have tried:
I have tried to implement the Dgv_CellPainting method, but I don't know what to use as the parameters, and I also don't know how to call it (what I mean is do I need to do something like DGV.Dgv_CellPainting(), ) I hope this makes sense.