代码之家  ›  专栏  ›  技术社区  ›  Andy

C#如何在不不断更改DefaultCellStyle的情况下为整行设置特定的单元格样式

  •  0
  • Andy  · 技术社区  · 15 年前

    我有一个对象列表(类型为“TrackedSet”),它是绑定到DataGridView的数据。添加新行时,我希望能够基于数据绑定对象的属性更改行外观。

    在下面的代码中,我通过不断地将行的DefaultCellStyle属性更改为预定义的单元格样式来改变这一点,该样式的前景色设置为我想要的。

    谢谢你的建议。

    /// <summary>
            /// Handles the drawing of each DataGridView row depending on TrackedSet error or flagged state.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void setLogDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            {
                if ((e.State & DataGridViewElementStates.Displayed) == DataGridViewElementStates.Displayed)
                {
                    // Always disable normal painting of selection background and focus.
                    e.PaintParts &= ~(DataGridViewPaintParts.SelectionBackground |
                                      DataGridViewPaintParts.Focus);
    
                    // Get the tracked set associated with the row being painted.
                    TrackedSet set = this._setLogBindingSource[e.RowIndex] as TrackedSet;
    
                    if (set.IsFlagged || set.IsError) // Row requires custom painting.
                    {
                        Color backColour1 = Color.Empty;
                        Color backColour2 = Color.Empty;
    
                        // Get rectangle of area to paint with custom brush.
                        Rectangle rowBounds = new Rectangle(
                            e.RowBounds.Left + 1,                                   // Location x
                            e.RowBounds.Top,                                        // Location y
                            this.setLogDataGridView.Columns.GetColumnsWidth(
                                DataGridViewElementStates.Visible) -
                            this.setLogDataGridView.HorizontalScrollingOffset + 1,  // Width
                            e.RowBounds.Height);                                    // Height
    
                        // Disable painting of backgrounds when custom painting row.
                        e.PaintParts &= ~(DataGridViewPaintParts.Background |
                                          DataGridViewPaintParts.ContentBackground);
    
                        if (set.IsFlagged) // Highlight colour.
                        {
                            backColour1 = this._highlightBackColour1;
                            backColour2 = this._highlightBackColour2;
                            this.setLogDataGridView.Rows[e.RowIndex].DefaultCellStyle = this._highlightStyle;
                        }
                        else // Error colour.
                        {
                            backColour1 = this._errorBackColour1;
                            backColour2 = this._errorBackColour2;
                            this.setLogDataGridView.Rows[e.RowIndex].DefaultCellStyle = this._errorStyle;
                        }
    
                        // Paint the custom background.
                        using (Brush lgb = new LinearGradientBrush(rowBounds, backColour1, backColour2, LinearGradientMode.Vertical))
                        {
                            e.Graphics.FillRectangle(lgb, rowBounds);
                        }
                    }
                }
            }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Adam Hughes    15 年前

    您可以尝试在中设置背景色和前景色 CellFormatting 事件

    void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        TrackedSet set = this._setLogBindingSource[e.RowIndex] as TrackedSet;
        if (set.IsFlagged)
        {
            e.CellStyle.BackColor = Color.Blue;
            e.CellStyle.ForeColor = Color.White;
        }
        else if (set.IsError)
        {
            e.CellStyle.BackColor = Color.Red;
            e.CellStyle.ForeColor = Color.Blue;
        }
    }
    

    要拥有自定义渐变背景,您可能仍然需要在 RowPrePaint ForeColor 单元格格式 事件

    DefaultCellStyle 在您将其绑定到DataGridView之后的行上,但我相信某些事件将触发DefaultCellStyle重置回 RowTemplate