如何在表单加载时让datagridview自动用圆圈填充每个单元格 [英] How to get datagridview to automatically fill each cell with a circle when the form loads

查看:114
本文介绍了如何在表单加载时让datagridview自动用圆圈填充每个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个看起来像LED显示屏的程序。我使用C#在Windows窗体中使用DataGridView。我的问题是我可以显示dataGridView,但它不会自动填充每个单元格中的圆圈。下面,您将看到Dgv_CellPainting方法,但我不知道如何以及在何处实现它。有人可以帮忙吗?以下是目前为止的所有代码...

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;                      
            
            // add 1536 cells
            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);
            }
     }
}





我尝试了什么:



我试图实现Dgv_CellPainting方法,但我不知道该用什么作为参数,我也不知道如何调用它(我的意思是我需要做一些像DGV.Dgv_CellPainting(),我希望这是有道理的。



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.

推荐答案

我认为 datagridview 不是最好的选择,这是一篇展示如何进行用户控制的文章: AC#LED矩阵显示 [ ^ ]
I think a datagridview is not the best choice, here is an article that shows how to make a user control: A C# LED matrix display[^]


这篇关于如何在表单加载时让datagridview自动用圆圈填充每个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆