开发者

Different colored lines in a DataGridView (DGV)

开发者 https://www.devze.com 2023-04-11 00:53 出处:网络
I ha开发者_Go百科ve a Data Grid View and am wondering if it is possible to highlight only certain rows that contain a specific column value.

I ha开发者_Go百科ve a Data Grid View and am wondering if it is possible to highlight only certain rows that contain a specific column value.

So I would like all of the rows to be defaulted to black with white text except for any row that has a specified column that equals FUJI/UNIVERSAL. For this row, I would like it to be yellow with black text.

So my question is.. is this possible? and if so, how?


The best place is the RowPrePaint event of the DataGridView.

Here is how you can achieve this.

void dataGridView1_RowPrePaint(object sender,
        DataGridViewRowPrePaintEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[0].Value == "YourConditionalValue")
    {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
    }
}


For example by handling the OnCellFormatting event.

Here's an actual piece of code from an old WinForms hobby project of mine, which does exactly that. It also teaches me again how important commenting is (no chance I would still remember it by now).

I' m sure you can adapt it to your purpose.

    private void sheetCalendar_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (CurrentRota == null)
        {
            return;
        }
        /* A word of explanation is needed. We retrieve the DataGridView from sender
         * and we access the cell in question directly. 
         * MSDN advices against it -
         * "to avoid performance penalties When handling this event,
         * access the cell through the parameters of the event handler
         * rather than accessing the cell directly."
         * 
         * The problem is that we don't want to set the same colour to the same cell
         * over and over again.
         * And e.CellStyle always "forgets" what colour the call had had! */
        var dgrid = sender as DataGridView;
        var _col = e.ColumnIndex; 
        var _row = e.RowIndex; 
        var _highlight = (((Rota) sheetCalendar.Rows[e.RowIndex].DataBoundItem).Monday ==
                          CurrentRota.Monday);
        var color = _highlight ?
                                   Settings.Default.DGRIDCalendar_CurrentlyEditedBackColor :
                                                                                               SystemColors.Window;
        if (dgrid[_col, _row].Style.BackColor != color)
            dgrid[_col, _row].Style.BackColor = color;

    }


Handle the OnPaint event for the DataGridView control.

Parse through the rows, and set the color on the rows that have the information you are looking for.

Some example code:

int specificColumnIndex = 5;
const string FUJIUNIV = "FUJI/UNIVERSAL";
const Color cFUJIBACK = Color.Yellow;
const Color cFUJITEXT = Color.Black;

public Form1() {
  InitializeComponent();
  dataGridView1.Paint += new PaintEventHandler(DataGridView_Paint);
}

private void DataGridView_Paint(object sender, PaintEventArgs e) {
  foreach (DataGridViewRow row in dataGridView1.Rows) {
    if (row.Cells[specificColumnIndex].Value.ToString() == FUJIUNIV) {
      foreach (DataGridViewCell cell in row.Cells) {
        cell.Style.BackColor = cFUJIBACK;
        cell.Style.ForeColor = cFUJITEXT;
        cell.Style.SelectionBackColor = Color.Gold;
      }
    }
  }
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号