Changing datagridview cell color dynamically

后端 未结 5 463
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 11:04

I have a dataGridView object that is populated with data. I want to click a button and have it change the color of the background of the cell. This is what I currently have<

相关标签:
5条回答
  • 2020-11-29 11:29

    Thanks it working

    here i am done with this by qty field is zero means it shown that cells are in red color

            int count = 0;
    
            foreach (DataGridViewRow row in ItemDg.Rows)
            {
                int qtyEntered = Convert.ToInt16(row.Cells[1].Value);
                if (qtyEntered <= 0)
                {
                    ItemDg[0, count].Style.BackColor = Color.Red;//to color the row
                    ItemDg[1, count].Style.BackColor = Color.Red;
    
                    ItemDg[0, count].ReadOnly = true;//qty should not be enter for 0 inventory                       
                }
                ItemDg[0, count].Value = "0";//assign a default value to quantity enter
                count++;
            }
    
        }
    
    0 讨论(0)
  • 2020-11-29 11:38

    Implement your own extension of DataGridViewTextBoxCell and override Paint method like this:

    class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
            DataGridViewElementStates cellState, object value, object formattedValue, string errorText,
            DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            if (value != null)
            {
                if ((bool) value)
                {
                    cellStyle.BackColor = Color.LightGreen;
                }
                else
                {
                    cellStyle.BackColor = Color.OrangeRed;
                }
            }
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
                formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
    }
    

    }

    Then in the code set CellTemplate property of your column to instance of your class

    columns.Add(new DataGridViewTextBoxColumn() {CellTemplate = new MyDataGridViewTextBoxCell()});
    
    0 讨论(0)
  • 2020-11-29 11:39

    Considere use DataBindingComplete event for update the style. The next code change the style of the cell:

        private void Grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.Grid.Rows[2].Cells[1].Style.BackColor = Color.Green;
        }
    
    0 讨论(0)
  • 2020-11-29 11:42

    This works for me

    dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
    
    0 讨论(0)
  • 2020-11-29 11:46

    If you want every cell in the grid to have the same background color, you can just do this:

    dataGridView1.DefaultCellStyle.BackColor = Color.Green;
    
    0 讨论(0)
提交回复
热议问题