Controls - Do you make the selected/enabled rows stand out in a datagrid?

Last updated by Thomas Iwainski [SSW] 12 months ago.See history

Many times you allow a multiple selection in a grid by using a checkbox. When you do this make it easy to see the distinction of a row that is selected and one that is not. Make it subtle by dimming the unselected text.

Figure: Bad example - Selected rows are not separate from others.

Figure: Good example - Selected rows are separate from others.

To make this effect in datagrid, you may need to edit the cellcontentclick event handler code. Example:

private void DatagridviewRules_CellContentClick(object sender, DataGridViewCellEventArgs e) {

  if (DatagridviewRules.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn && e.ColumnIndex == 0 && e.RowIndex != -1) {

    bool boolCheckBox = (bool)(DatagridviewRules.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);

    DatagridviewRules.Rows[e.RowIndex].DefaultCellStyle.ForeColor = boolCheckBox ? SystemColors.WindowText : SystemColors.ControlDark;

    DataRowView objDataRowView = (DataRowView) DatagridviewRules.Rows[e.RowIndex].DataBoundItem;

    JobRule.DataTableJobRulesRow objDataRow = (JobRule.DataTableJobRulesRow)(objDataRowView.Row);

    updateRuleIsEnabled(objDataRow.RuleId, boolCheckBox);
    updateSelectAllCheckBox();
    updateRulesCount();
  }

}

Setting the ForeColor to different ones, like black and gray, can separate the selected rows from others.

We open source. Powered by GitHub