Making a DataGridView look like an Excel sheet


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!

I need to learn the DGV control someday :)
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;

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


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!

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

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