How to deselect all selected rows in a DataGridView control?

前端 未结 6 633
臣服心动
臣服心动 2021-01-31 01:24

I\'d like to deselect all selected rows in a DataGridView control when the user clicks on a blank (non-row) part of the control.
How can I do this?

6条回答
  •  礼貌的吻别
    2021-01-31 02:25

    i have ran into the same problem and found a solution (not totally by myself, but there is the internet for)

    Color blue  = ColorTranslator.FromHtml("#CCFFFF");
    Color red = ColorTranslator.FromHtml("#FFCCFF");
    Color letters = Color.Black;
    
    foreach (DataGridViewRow r in datagridIncome.Rows)
    {
        if (r.Cells[5].Value.ToString().Contains("1")) { 
            r.DefaultCellStyle.BackColor = blue;
            r.DefaultCellStyle.SelectionBackColor = blue;
            r.DefaultCellStyle.SelectionForeColor = letters;
        }
        else { 
            r.DefaultCellStyle.BackColor = red;
            r.DefaultCellStyle.SelectionBackColor = red;
            r.DefaultCellStyle.SelectionForeColor = letters;
        }
    }
    

    This is a small trick, the only way you can see a row is selected, is by the very first column (not column[0], but the one therefore). When you click another row, you will not see the blue selection anymore, only the arrow indicates which row have selected. As you understand, I use rowSelection in my gridview.

提交回复
热议问题