Domanda

Ho 2 variabili Vorrei visualizzare in una cella di un DataGridView.

icon stockicon; Int StockStatus;

Ho già guardato http://msdn.microsoft.com/en-US / Biblioteca / 7Tas5C80.aspx Ma penso che la sua strada complicasse e non mostrare come visualizzare in variabili in una cella.

Non ho bisogno della possibilità di modificare, visualizzare solo le due variabili.

Qualcuno potrebbe fornirmi un piccolo esempio?

Lavoro in c # 4.0 e è un sistema.Windows.Forms.DataGridView

È stato utile?

Soluzione

Ecco la mia soluzione.Basta impostare il tipo di colonna in LagerstusUnolumn e ottiene il lavoro fatto.

 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);
        }
    }
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top