Btw. this is not a sudoku solver.
To gain a deeper insight into the workings of the DataGridView beast (but I begin to love and appreciate it) I decided to make myself a sudoku layout with it.
Start a forms app. Make the form a bit bigger and fill in the code. I did most of it in the load event of the form. It was fun to do, but the real fun starts now:
How to handle the myriad of events of the DataGridView to fill up the board with digits? For the moment I can fill in only one character in a cell(see line 47),but I can also input letters. How to avoid this?
How can I jump the inputfield over an already filled in digit when I push an arrowkey? Etc. etc.
Let you know when I finished that in a couple of months or so. :o)
Making a DataGridView look like a sudoku board
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SudokuStuff
{
public partial class Form1 : Form
{
const int cColWidth = 45;
const int cRowHeigth = 45;
const int cMaxCell = 9;
const int cSidelength = cColWidth * cMaxCell + 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(cSidelength, cSidelength);
DGV.Location = new Point(50, 50);
DGV.Font = new System.Drawing.Font("Calibri", 16.2F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
DGV.ForeColor = Color.DarkBlue;
DGV.SelectionMode = DataGridViewSelectionMode.CellSelect;
// add 81 cells
for (int i = 0; i < cMaxCell; i++)
{
DataGridViewTextBoxColumn TxCol = new DataGridViewTextBoxColumn();
TxCol.MaxInputLength = 1; // only one digit allowed in a cell
DGV.Columns.Add(TxCol);
DGV.Columns[i].Name = "Col " + (i + 1).ToString();
DGV.Columns[i].Width = cColWidth;
DGV.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
DataGridViewRow row = new DataGridViewRow();
row.Height = cRowHeigth;
DGV.Rows.Add(row);
}
// mark the 9 square areas consisting of 9 cells
DGV.Columns[2].DividerWidth = 2;
DGV.Columns[5].DividerWidth = 2;
DGV.Rows[2].DividerHeight = 2;
DGV.Rows[5].DividerHeight = 2;
Controls.Add(DGV);
}
}
}
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.