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;
                }
            }
        }
    }

 

posted on 2018-04-24 20:43  武胜-阿伟  阅读(377)  评论(0编辑  收藏  举报