Making a DataGridView look like an Excel sheet

ddanbe 2 Tallied Votes 4K Views Share

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!

Momerath commented: I need to learn the DGV control someday :) +13
using System;
using System.Drawing;
using System.Windows.Forms;

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

        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            InitializeDataGridView(5, 6);

        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++)
            dgv.RowHeadersDefaultCellStyle.Padding = new Padding(3);//helps to get rid of the selection triangle?
            for (int i = 0; i < rows; 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();

        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;
Priti_1 0 Newbie Poster

thank.its nice task.
Can i delete a gridview row like deleted in excel,just by right clicking

ddanbe 2,724 Professional Procrastinator Featured Poster

Hi Priti 1, welcome to DaniWeb! :)
For what you're asking, yes but.
You have to set line 25 here to true and implement a MouseDown event handler to do it.
Happy programming!

jessechunn 0 Newbie Poster

Great... Even 5 years later I found this and used it. Thanks.
Consider adding this code to the beginning of the InitializeDataGridView method to eliminate the slow redraw of the datagrid:

typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null,
dgv, new object[] { true });

Be a part of the DaniWeb community

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