I need to draw a line grid on a windows form that contains 4 rows and 4 columns.
Have a look at: How to: Draw a Line on a Windows Form
If you want something for controlling layout, rather than a simple line grid, you can use a TableLayoutPanel.
In response to your comment, you can achieve what you want using the TableLayoutPanel and anchoring and docking. There is also the FlowLayoutPanel, but be careful not to overuse this control, as form load speed appears to suffer.
I would override the OnPaint method and add in some code to draw the lines in the background. Then, after I finish the drawing I want to do, I would call the base.OnPaint to let the form continue drawing any other controls that may be on the form. This way, the lines are in the background and will not be painted on top of any other controls. Also, be sure the add a graphics.clear() call to avoid the screen tearing effect.
Perhaps you can try a variant of this code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Pen linePen = new Pen(System.Drawing.Color.CornflowerBlue);
Graphics grphx = this.CreateGraphics();
grphx.Clear(this.BackColor);
for(int i=1; i<5; i++)
{
//Draw verticle line
grphx.DrawLine(linePen,
(this.ClientSize.Width/4)*i,
0,
(this.ClientSize.Width / 4) * i,
this.ClientSize.Height);
//Draw horizontal line
grphx.DrawLine(linePen,
0,
(this.ClientSize.Height / 4) * i,
this.ClientSize.Width,
(this.ClientSize.Height / 4) * i);
}
linePen.Dispose();
//Continues the paint of other elements and controls
base.OnPaint(e);
}
}
}