Отобразить значок и строку внутри ячейки в DataGridView - PullRequest
3 голосов
/ 13 июля 2010

У меня есть 2 переменные, которые я хотел бы отобразить в одной ячейке DataGridView.

Icon stockIcon;Int stockStatus;

Я уже посмотрел на http://msdn.microsoft.com/en-us/library/7tas5c80.aspx Но я думаю, что это сложный путь, и я не показываю, как отображать переменные в одной ячейке.

Не знаюнужна возможность редактировать, отображать только две переменные.

Может ли кто-нибудь предоставить мне небольшой пример?

Я работаю в C # 4.0 и его System.Windows.Forms.DataGridView

1 Ответ

4 голосов
/ 04 августа 2010

Вот мое собственное решение. Просто установите тип столбца в LagerStatusColumn, и он выполнит свою работу.

 public class LagerStatusColumn : DataGridViewColumn
{
    public LagerStatusColumn()
    {
        CellTemplate =
            new LagerStatusCell();
        ReadOnly = true;
    }
}
 public class LagerStatusCell : DataGridViewTextBoxCell
{
    protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, "", errorText, cellStyle,
                   advancedBorderStyle, paintParts);

        var cellValue = Convert.IsDBNull(value) ? 0 : Convert.ToDecimal(value);

        const int horizontaloffset = 2;

        var parent = (LagerStatusColumn)this.OwningColumn;

        var fnt = parent.InheritedStyle.Font;

        var icon = Properties.Resources.lager;
        if (cellValue == 0)
            icon = Properties.Resources.rest;
        else if (cellValue < 0)
            icon = Properties.Resources.question_white;

        const int vertoffset = 0;
        graphics.DrawIcon(icon, cellBounds.X + horizontaloffset,
             cellBounds.Y + vertoffset);

        var cellText = formattedValue.ToString();
        var textSize =
            graphics.MeasureString(cellText, fnt);

        //  Calculate the correct color:
        var textColor = parent.InheritedStyle.ForeColor;
        if ((cellState &
             DataGridViewElementStates.Selected) ==
            DataGridViewElementStates.Selected)
        {
            textColor = parent.InheritedStyle.
                SelectionForeColor;
        }

        // Draw the text:
        using (var brush = new SolidBrush(textColor))
        {
            graphics.DrawString(cellText, fnt, brush,
                                cellBounds.X + icon.Width + 2,
                                cellBounds.Y + 0);
        }
    }
}
...