Draw any grid you want.

ddanbe 0 Tallied Votes 1K Views Share

This is not about a DataGridView or something else, it is just about drawing a simple grid in a form window.
Googled a bit but found not much of my liking. A Grid as a BitMap in a PictureBox?
Come on! Must have been some of those GUI guys I guess. I find it easier to draw in a Form or a Panel in a Form, so I have total control over where to draw a chess piece or a Sudoku number or whatever. Or I could draw a graph with a grid; think it’s harder to do that with a BitMap.
So I decided to make my own simple grid class.
And so, here is the third part of my “any” code snippets, the other two being:
http://www.daniweb.com/software-development/csharp/code/217206
http://www.daniweb.com/software-development/csharp/code/360165
Make a new Forms application, drop a Panel control on the Form. Make the Panel Size about 350,350 and set its BorderStyle to FixedSingle(just to see where it is in the form).
Add the code for the Grid class to your project and fill in the code for the Form.cs file.
I use the Paint event handler of the Panel control.
Enjoy!

// class that can draw a grid
    class Grid
    {
        public Grid()
        {
            // Set some defaults
            Origin = new Point(0, 0);
            GridCellSize = new Size(10, 10);
            HorizontalCells = 1;
            VerticalCells = 1;
        }

        public int HorizontalCells { get; set; }

        public int VerticalCells { get; set; }

        public Point Origin { get; set; }

        public Size GridCellSize { get; set; }

        public virtual void Draw(Graphics Graf, Pen pencil)
        {
            Point startP = new Point();
            Point endP = new Point();

            // Draw horizontals
            startP.X = Origin.X;
            endP.X = Origin.X + VerticalCells * GridCellSize.Width;
            for (int i = 0; i <= HorizontalCells; i++)
            {
                startP.Y = Origin.Y + i * GridCellSize.Height;
                endP.Y = startP.Y;
                Graf.DrawLine(pencil, startP, endP);
            }

            // Draw verticals
            startP.Y = Origin.Y;
            endP.Y = Origin.Y + HorizontalCells * GridCellSize.Height;
            for (int i = 0; i <= VerticalCells; i++)
            {
                startP.X = Origin.X + i * GridCellSize.Width;
                endP.X = startP.X;
                Graf.DrawLine(pencil, startP, endP);
            }
        }
    }

// code for the form
    public partial class Form1 : Form
    {
        Grid thisGrid = new Grid();

        public Form1()
        {
            InitializeComponent();
            // Init a grid
            thisGrid.Origin = new Point(5, 5);
            thisGrid.GridCellSize = new Size(40, 50);
            thisGrid.HorizontalCells = 5;
            thisGrid.VerticalCells = 7;
        }
       
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            // Make some fancy pen, make ANY pen you like here
            Pen myPen = new Pen(Color.Blue, 2f);
            myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            myPen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle;

            thisGrid.Draw(e.Graphics, myPen);
        }
    }
naimeh 0 Newbie Poster

hello.i 'm new in c sharp.and i use this code...but it don't draw the grid :(
why?

ddanbe 2,724 Professional Procrastinator Featured Poster

Hi naimeh welcome here! :)
Did you follow all the instructions?
Could you send your code?

naimeh_m 0 Newbie Poster

thanks...
my problem solved!!!

Wazaah 0 Newbie Poster

I followed the instructions but the code won't work for me :(

ddanbe 2,724 Professional Procrastinator Featured Poster

Hi, Wazaah welcome! :)
Could you explain wath you did and send some code of yours?

Wazaah 0 Newbie Poster

It still doesn't work, but i found another example that worked for me :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.