DataGridView 在 WinForms中应用不同的单元格式

/// <summary>
/// Set the cell background colour to make the ups and downs more visible.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null && e.RowIndex >= 0)
        {
        if (e.ColumnIndex > 0)
            {
            Color c;
            if (e.Value != null && e.Value is Int32)
                {
                int value = (int) e.Value;
                if (value > 0)
                    {
                    c = Color.LightGreen;
                    }
                else if (value < 0)
                    {
                    c = Color.Salmon;
                    }
                else
                    {
                    c = dgv.DefaultCellStyle.BackColor;
                    }
                e.CellStyle.BackColor = c;
                }
            }
        }
    }
原文地址:https://www.cnblogs.com/zeroone/p/8933566.html