Laden...

Gefilterte Spalte oder Header im DataGridView farbig hinterlegen

Erstellt von Edzio vor 9 Jahren Letzter Beitrag vor 9 Jahren 1.411 Views
E
Edzio Themenstarter:in
68 Beiträge seit 2013
vor 9 Jahren

jetzt noch eine kurze Frage,
wenn ich nun eine Spalte filtere, dann meinetwegen nochmal nach einer filtere
wie kann ich die entsprechende headerzelle oder auch die gefilterte spalte farbig hinterlegen?
Google meinte dass man in der Paint Methode der DGVAutoFilterHeaderCells.cs bearbeiten etwas mit BackColor hinzufügen muss, wenn ich das mache passiert aber nichts, obwohl ich EnableHeadersVisualStyle auf false gesetzt habe.

In die Klasse habe ich eine if schleife eingefügt:

 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)
        {
            //von mir eingefügt um den gefiltertetn hintergrund zu visualisieren
            if (this.filtered)
            {
                cellStyle.BackColor = Color.Brown;
            }
            // Use the base method to paint the default appearance.

                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    cellState, value, formattedValue,
                    errorText, cellStyle, advancedBorderStyle, paintParts);

Muss ich da aus meiner Form1.cs noch was einfügen
wie das beim einfügen des AutoFilter?

dGvexcel.BindingContextChanged += new EventHandler(dGvexcel_BindingContextChanged);
2.207 Beiträge seit 2011
vor 9 Jahren
E
Edzio Themenstarter:in
68 Beiträge seit 2013
vor 9 Jahren

also nachdem ich einen Tip bekommen habe klappt es nun.
Es liegt daran, wie man die "DataGridViewAutoFilterColumnHeaderCell.cs"
integriert hat, entweder über die .dll
oder direkt den Verweis auf die .cs
Hat man die .dll muss das Projekt in der sich die .cs Datei befindet geöffnet werden und dort den gleich folgenden Code einfügen, anschließend das Projekt neu generieren, so dass die Änderungen alles compailiert werden und in der .dll wirksam sind.
Ansonsten muss man die integrierte .cs Datei im Projektmappenexplorer öffnen und den Code einfügen.

Sucht in diese .cs Datei die Stelle an der die Paint funktion aufgerufen wird, und fügt dort die If-Abfrage ein, die nach meinem Kommentar ("//von mir eingefügt um den gefiltertetn hintergrund zu visualisieren") steht :


        /// <summary>
        /// Paints the column header cell, including the drop-down button. 
        /// </summary>
        /// <param name="graphics">The Graphics used to paint the DataGridViewCell.</param>
        /// <param name="clipBounds">A Rectangle that represents the area of the DataGridView that needs to be repainted.</param>
        /// <param name="cellBounds">A Rectangle that contains the bounds of the DataGridViewCell that is being painted.</param>
        /// <param name="rowIndex">The row index of the cell that is being painted.</param>
        /// <param name="cellState">A bitwise combination of DataGridViewElementStates values that specifies the state of the cell.</param>
        /// <param name="value">The data of the DataGridViewCell that is being painted.</param>
        /// <param name="formattedValue">The formatted data of the DataGridViewCell that is being painted.</param>
        /// <param name="errorText">An error message that is associated with the cell.</param>
        /// <param name="cellStyle">A DataGridViewCellStyle that contains formatting and style information about the cell.</param>
        /// <param name="advancedBorderStyle">A DataGridViewAdvancedBorderStyle that contains border styles for the cell that is being painted.</param>
        /// <param name="paintParts">A bitwise combination of the DataGridViewPaintParts values that specifies which parts of the cell need to be painted.</param>
        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)
        {
            //von mir eingefügt um den gefiltertetn hintergrund zu visualisieren
            if (this.currentColumnFilter != "")
            {
                cellStyle.BackColor = Color.DimGray;
                cellStyle.ForeColor = Color.White;
            }
            // Use the base method to paint the default appearance. 
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, 
                cellState, value, formattedValue, 
                errorText, cellStyle, advancedBorderStyle, paintParts);

            // Continue only if filtering is enabled and ContentBackground is 
            // part of the paint request. 

Gruß