Hello,

I have create a program in C# windows application & I want to chage the color of datagrid view. Like backcolor,fieldcolor

Edited 6 Years Ago by virusisfound: n/a

You can change Column(using column index) background color u can use the following code line :

gridResults.Columns[0].DefaultCellStyle.BackColor = Color.Blue;

but for changing Row(using row index) background color use the following code line :

gridResults.Rows[0].DefaultCellStyle.BackColor = Color.Blue;

This is some code from something I'm still working on, look at the color thingies:

#region Initialisation of the DataGridView

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

            this.Location = new System.Drawing.Point(0, 0);
            this.Name = "dataGridView1";
            this.Size = new System.Drawing.Size(250, 125);
            this.TabIndex = 0;
            this.RowHeadersWidth = 55;
            //used to attach event-handlers to the events of the editing control(nice name!)
            this.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(this.MatrixIO_EditingControlShowing);

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

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

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

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

            this.ShowEditingIcon = false;
            this.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

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

        private void AddAColumn(int i)
        {
            DataGridViewTextBoxColumn Acolumn = new DataGridViewTextBoxColumn();
            Acolumn.HeaderText = "C" + i.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;
            this.Columns.Add(Acolumn);
        }

        public void MakeMatrixTitle(string Title)
        {
            this.TopLeftHeaderCell.Value = Title;
            this.TopLeftHeaderCell.Style.BackColor = Color.AliceBlue;
        }

        #endregion
This article has been dead for over six months. Start a new discussion instead.