1.11M Members

Making a DataGridView look like an Excel sheet

 
2
 

OK, not completely, I admit. I used some different colors(colours) here and there. But this code will put you on the way.
This is a little modification of code I use to show a matrix in a form.
Just start a new Forms app and fill in the code. Enjoy!

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ExcelGrid
{
    public partial class Form1 : Form
    {
        DataGridView dgv = new DataGridView();

        public Form1()
        {
            InitializeComponent();            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeDataGridView(5, 6);
            this.Controls.Add(dgv);
        }

        private void InitializeDataGridView(int rows, int columns)
        {
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dgv.AllowUserToResizeRows = false;
            dgv.EnableHeadersVisualStyles = false;
            dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgv.EditMode = DataGridViewEditMode.EditOnKeystroke;
            dgv.ShowEditingIcon = false;

            dgv.Location = new System.Drawing.Point(0, 0);
            dgv.Name = "dataGridView1";
            dgv.Size = new System.Drawing.Size(250, 125);
            dgv.TabIndex = 0;
            dgv.RowHeadersWidth = 55;
            //used to attach event-handlers to the events of the editing control(nice name!)
            //dgv.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(Mydgv_EditingControlShowing);
            // not implemented here, but I still like the name DataGridViewEditingControlShowingEventHandler :o) LOL

            dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

            for (int i = 0; i < columns; i++)
            {
                AddAColumn(i);
            }
            dgv.RowHeadersDefaultCellStyle.Padding = new Padding(3);//helps to get rid of the selection triangle?
            for (int i = 0; i < rows; i++)
            {
                AddARow(i);
            }

            dgv.ColumnHeadersDefaultCellStyle.Font = new Font("Verdana", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.ColumnHeadersDefaultCellStyle.BackColor = Color.Gainsboro;

            dgv.RowHeadersDefaultCellStyle.Font = new Font("Verdana", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            dgv.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.RowHeadersDefaultCellStyle.BackColor = Color.Gainsboro;
        }

        private void AddARow(int i)
        {
            DataGridViewRow Arow = new DataGridViewRow();
            Arow.HeaderCell.Value = i.ToString();
            dgv.Rows.Add(Arow);
        }

        private void AddAColumn(int i)
        {
            DataGridViewTextBoxColumn Acolumn = new DataGridViewTextBoxColumn();
            //OK I know this only works normally for 26 chars(columns)
            // I leave the rest of the Excel columns up to you to figure out :o)
            char ch = (char)(i + 65);
            Acolumn.HeaderText = ch.ToString();
            Acolumn.Name = "Column" + i.ToString();
            Acolumn.Width = 60;
            Acolumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            //make a Style template to be used in the grid
            DataGridViewCell Acell = new DataGridViewTextBoxCell();
            Acell.Style.BackColor = Color.LightCyan;
            Acell.Style.SelectionBackColor = Color.FromArgb(128, 255, 255);
            Acolumn.CellTemplate = Acell;
            dgv.Columns.Add(Acolumn);
        }
    }
}
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This is an OP Kudos discussion and contributors may be rewarded
Post:
Start New Discussion
View similar articles that have also been tagged: