Extended-Toolkit icon indicating copy to clipboard operation
Extended-Toolkit copied to clipboard

[Feature Request]: Alphablend selection in DataGridView

Open mikel3361 opened this issue 3 years ago • 12 comments

Its possible to Alphablend selection in DataGridView so colors in cells will be preserved when selected?

Currently use a helper class to do a semialphablend inside, but i plan to use multiple colors in one cell and this aproach wont work

KryptonDataGridViewHelper.zip

mikel3361 avatar Jun 10 '22 11:06 mikel3361

Please split the request into 2 items (So that you can track them)

Smurf-IV avatar Jun 10 '22 14:06 Smurf-IV

Done

mikel3361 avatar Jun 13 '22 09:06 mikel3361

Based on the following article, I solved it this way for now. (see attachment)

Helper contains Semi-Alphablending, Hide-Selection and Row-Numbering - this inside the grid with properties would be nice...

Preview: image

KryptonDataGridViewHelper.zip (VB)

mikel3361 avatar Jun 23 '22 06:06 mikel3361

Solved with a helper class!

mikel3361 avatar Jul 27 '22 14:07 mikel3361

Solved with a helper class!

Can this be added to the "Extended Toolkits" version of the Data grid please ?

Smurf-IV avatar Jul 31 '22 15:07 Smurf-IV

Solved with a helper class!

Can this be added to the "Extended Toolkits" version of the Data grid please ?

Needs to be converted from VB to C#

PWagner1 avatar Jul 31 '22 15:07 PWagner1

Helper based on following articles: Semi-Transparent Selection --> https://stackoverflow.com/questions/38337849/transparent-selectionbackcolor-for-datagridview-cell Hide-Selection the same, but using Cell Color as SelectionColor Auto-Rownumbers --> https://stackoverflow.com/questions/9581626/show-row-number-in-row-header-of-a-datagridview

articles uses c# so you can do some work, color calculating needs perhaps a little cleanup (calcualating darker backcolor) - forecolor always cellforecolor with fallbacks to row and col. (Instead of adding an additional event, perhaps you can redefine the events over a property)

mikel3361 avatar Aug 01 '22 07:08 mikel3361

using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using System.Drawing; using System.Windows.Forms; using Krypton.Toolkit;`

public class KryptonDataGridViewHelper { public static void EnableSemiTransparentSelection(KryptonDataGridView dataGridView) { dataGridView.RowPrePaint += GridSemiTransparentSelection_RowPrePaint; }

public static void DisableSemiTransparentSelection(KryptonDataGridView dataGridView)
{
    dataGridView.RowPrePaint -= GridSemiTransparentSelection_RowPrePaint;
}

public static void EnableHideSelection(KryptonDataGridView dataGridView)
{
    dataGridView.RowPrePaint += GridHideSelection_RowPrePaint;
}

public static void DisableHideSelection(KryptonDataGridView dataGridView)
{
    dataGridView.RowPrePaint -= GridHideSelection_RowPrePaint;
}

public static void EnableRowNumbers(KryptonDataGridView dataGridView)
{
    dataGridView.RowPostPaint += GridShowRowNumbers_RowPostPaint;
}

public static void DisableRowNumbers(KryptonDataGridView dataGridView)
{
    dataGridView.RowPostPaint -= GridShowRowNumbers_RowPostPaint;
}

private static void GridSemiTransparentSelection_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    KryptonDataGridView dataGridView = sender as KryptonDataGridView;

    foreach (DataGridViewCell cell in dataGridView.Rows(e.RowIndex).Cells)
    {
        if (cell.Selected == false)
            continue;

        Color bgColorCell = Color.Empty;
        Color fgColorCell = Color.Empty;

        if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor != Color.Empty)
            bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor;
        if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor != Color.Empty)
            bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor;
        if (cell.Style.BackColor != Color.Empty)
            bgColorCell = cell.Style.BackColor;

        if (dataGridView.Rows(e.RowIndex).DefaultCellStyle.ForeColor != Color.Empty)
            fgColorCell = dataGridView.Rows(e.RowIndex).DefaultCellStyle.ForeColor;
        if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.ForeColor != Color.Empty)
            bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.ForeColor;
        if (cell.Style.ForeColor != Color.Empty)
            fgColorCell = cell.Style.ForeColor;

        if (bgColorCell == Color.Empty)
            cell.Style.SelectionBackColor = dataGridView.DefaultCellStyle.SelectionBackColor;
        else
            cell.Style.SelectionBackColor = ControlPaint.LightLight(ControlPaint.LightLight(ControlPaint.Dark(bgColorCell)));
        if (fgColorCell == Color.Empty)
            cell.Style.SelectionForeColor = dataGridView.DefaultCellStyle.SelectionForeColor;
        else
            cell.Style.SelectionForeColor = fgColorCell;
    }
}

private static void GridHideSelection_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    KryptonDataGridView dataGridView = sender as KryptonDataGridView;

    foreach (DataGridViewCell cell in dataGridView.Rows(e.RowIndex).Cells)
    {
        if (cell.Selected == false)
            continue;

        Color bgColorCell = Color.Empty;
        Color fgColorCell = Color.Empty;

        if (dataGridView.DefaultCellStyle.BackColor != Color.Empty)
            bgColorCell = dataGridView.DefaultCellStyle.BackColor;
        if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor != Color.Empty)
            bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor;
        if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor != Color.Empty)
            bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor;
        if (cell.Style.BackColor != Color.Empty)
            bgColorCell = cell.Style.BackColor;

        if (dataGridView.DefaultCellStyle.ForeColor != Color.Empty)
            fgColorCell = dataGridView.DefaultCellStyle.ForeColor;
        if (dataGridView.Rows(e.RowIndex).DefaultCellStyle.ForeColor != Color.Empty)
            fgColorCell = dataGridView.Rows(e.RowIndex).DefaultCellStyle.ForeColor;
        if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.ForeColor != Color.Empty)
            bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.ForeColor;
        if (cell.Style.ForeColor != Color.Empty)
            fgColorCell = cell.Style.ForeColor;

        if (bgColorCell != Color.Empty)
            cell.Style.SelectionBackColor = bgColorCell;
        if (fgColorCell != Color.Empty)
            cell.Style.SelectionForeColor = fgColorCell;
    }
}

public static void GridShowRowNumbers_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{

    // Automatically maintains a Row Header Index Number 
    // like the Excel row number, independent of sort order

    KryptonDataGridView grid = (KryptonDataGridView)sender;
    string rowIdx = (e.RowIndex + 1).ToString();
    StringFormat centerFormat = new StringFormat()
    {
        Alignment = StringAlignment.Far,
        LineAlignment = StringAlignment.Near
    };

    Rectangle headerBounds = new Rectangle(e.RowBounds.Left + 2, e.RowBounds.Top + 2, grid.RowHeadersWidth - 4, e.RowBounds.Height - 4);
    e.Graphics.DrawString(rowIdx, grid.DefaultCellStyle.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}

}

(Fixed Hide Selection for cell colors)

mikel3361 avatar Aug 01 '22 08:08 mikel3361

Converted my Helper Class to c# with https://converter.telerik.com/ from my current version! Style priorities should be: cell - column - row - default if you put it directly inside event!

mikel3361 avatar Aug 01 '22 08:08 mikel3361

Fixed Helper code for Hide Selection cell colors ...

With HideSelection (No Selection visible): image

With Transparent (Selection using Selection Colors or Fore/Backcolor): image

Screenshot shows also Row numbers.

mikel3361 avatar Aug 02 '22 09:08 mikel3361

Working with the selection i get a little backcolor problem on systemcolors.window - fixed it.

`private static void GridSemiTransparentSelection_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { KryptonDataGridView dataGridView = sender as KryptonDataGridView;

foreach (DataGridViewCell cell in dataGridView.Rows(e.RowIndex).Cells)
{
    if (cell.Selected == false)
        continue;

    Color bgColorCell = Color.Empty;
    Color fgColorCell = Color.Empty;

    if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor != Color.Empty)
        bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor;
    if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor != Color.Empty)
        bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.BackColor;
    if (cell.Style.BackColor != Color.Empty)
        bgColorCell = cell.Style.BackColor;

    if (dataGridView.Rows(e.RowIndex).DefaultCellStyle.ForeColor != Color.Empty)
        fgColorCell = dataGridView.Rows(e.RowIndex).DefaultCellStyle.ForeColor;
    if (dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.ForeColor != Color.Empty)
        bgColorCell = dataGridView.Columns.Item(cell.ColumnIndex).DefaultCellStyle.ForeColor;
    if (cell.Style.ForeColor != Color.Empty)
        fgColorCell = cell.Style.ForeColor;

    if (bgColorCell != Color.Empty && bgColorCell != SystemColors.Window)
        cell.Style.SelectionBackColor = ControlPaint.LightLight(ControlPaint.LightLight(ControlPaint.Dark(bgColorCell)));
    if (fgColorCell != Color.Empty)
        cell.Style.SelectionForeColor = fgColorCell;
}

} `

mikel3361 avatar Aug 04 '22 05:08 mikel3361

Add a new feature request for this - solved over inherit... #345

mikel3361 avatar Aug 08 '22 11:08 mikel3361