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

时间:2009-08-26 10:43:30

标签: c# .net datagridview

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

我可以在RowPrePaint事件中绘制该行的自定义背景,但我正在努力改变该行的前景色。

从下面的代码中,我通过不断将行的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 个答案:

答案 0 :(得分:1)

您可以尝试在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事件中设置CellFormatting

您也可以在将其绑定到DataGridView后尝试在行上设置DefaultCellStyle,但我相信某些事件会触发DefaultCellStyle重置回RowTemplate。将数据绑定到网格后,您可以在RowTemplate上设置DefaultCellStyle,这样就不会重置单元格样式。