Big Al Ruby Newbie
Big Al Ruby Newbie

Reputation: 834

How to Dynamically Create a Grid in C# Windows Form Application

I have a project that needs to have a grid dynamically created on a Windows Form Application that has a click listener on each cell.

The Grid needs to be 12 X 12

I have researched and researched this and cant find a way to add a grid to a Windows Form Application. I have seen solutions for WPF but not for windows Forms.

If you Can point me in the right direction would appreciate any help

The grid will need to be used with a Battleship Game

Upvotes: 1

Views: 34310

Answers (2)

Keith Holloway
Keith Holloway

Reputation: 1106

Try this code in the form on which you want the grid:

EDIT: I added a way to put 12 rows of 12 columns in the grid via the new BattleShipRow class. You can add data to a grid in numerous ways, but this binds a list of those objects to the grid.

You will likely have to tweak the sizes to make the display the way you would like.

Finally, I made the battleShipGrid list that is defined in LoadGridData a public member. You can modify the data in this list of BattleShipRow objects as needed for the game as clicks occur.

    private System.Windows.Forms.DataGridView myNewGrid;  // Declare a grid for this form
    private List<BattleShipRow> battleShipGrid; // Declare this here so that you can use it later to manipulate the cell contents

    private void Form1_Load(object sender, EventArgs e)
    {
        myNewGrid = new System.Windows.Forms.DataGridView();
        ((System.ComponentModel.ISupportInitialize)(myNewGrid)).BeginInit();
        this.SuspendLayout();
        myNewGrid.Parent = this;  // You have to set the parent manually so that the grid is displayed on the form
        myNewGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        myNewGrid.Location = new System.Drawing.Point(10, 10);  // You will need to calculate this postion based on your other controls.  
        myNewGrid.Name = "myNewGrid";
        myNewGrid.Size = new System.Drawing.Size(400, 400);  // You said you need the grid to be 12x12.  You can change the size here.
        myNewGrid.TabIndex = 0;
        myNewGrid.ColumnHeadersVisible = false; // You could turn this back on if you wanted, but this hides the headers that would say, "Cell1, Cell2...."
        myNewGrid.RowHeadersVisible = false;
        myNewGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        myNewGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
        myNewGrid.CellClick += MyNewGrid_CellClick;  // Set up an event handler for CellClick.  You handle this in the MyNewGrid_CellClick method, below
        ((System.ComponentModel.ISupportInitialize)(myNewGrid)).EndInit();
        this.ResumeLayout(false);
        myNewGrid.Visible = true;
        LoadGridData();
    }

    public class BattleShipRow
    {
        public string Cell1 { get; set; }
        public string Cell2 { get; set; }
        public string Cell3 { get; set; }
        public string Cell4 { get; set; }
        public string Cell5 { get; set; }
        public string Cell6 { get; set; }
        public string Cell7 { get; set; }
        public string Cell8 { get; set; }
        public string Cell9 { get; set; }
        public string Cell10 { get; set; }
        public string Cell11 { get; set; }
        public string Cell12 { get; set; }
    }

    private void LoadGridData()
    {
        battleShipGrid = new List<BattleShipRow>();
        for (var i = 0; i < 12; i++)
        {
            battleShipGrid.Add(new BattleShipRow());
        }
        myNewGrid.DataSource = battleShipGrid;
    }

    private void MyNewGrid_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        throw new NotImplementedException();
    }

Upvotes: 7

Always Learning
Always Learning

Reputation: 302

For Win Forms, you are looking for a DataGridView. Here are multiple ways of going about it, depending on if you want to do via js or code behind.

How do I dynamically create a DataGridView in C#?

Programmatically create DataGridview from DataTable

How to dynamically create columns in a datagridview and assign titles to it and its rows?

Upvotes: 0

Related Questions