Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView selected cell style

Tags:

c#

winforms

How can I change the "selection style" on a DataGridView (winforms)?

like image 602
Yanko Hernández Alvarez Avatar asked Jan 24 '23 11:01

Yanko Hernández Alvarez


1 Answers

You can easily change the forecolor and backcolor of selcted cells by assigning values to the SelectedBackColor and SelectedForeColor of the Grid's DefaultCellStyle.

If you need to do any further styling you you need to handle the SelectionChanged event

Edit: (Other code sample had errors, adjusting for multiple selected cells [as in fullrowselect])

using System.Drawing.Font;

private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {

            foreach(DataGridViewCell cell in ((DataGridView)sender).SelectedCells)
        {
            cell.Style = new DataGridViewCellStyle()
            {
                BackColor = Color.White,
                Font = new Font("Tahoma", 8F),
                ForeColor = SystemColors.WindowText,
                SelectionBackColor = Color.Red,
                SelectionForeColor = SystemColors.HighlightText
            };
        }
        }
like image 59
Luis Avatar answered Feb 04 '23 11:02

Luis