Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DataGridView中的单元格内显示图标和字符串_C#_Datagridview_Datagridviewcolumn - Fatal编程技术网

C# 在DataGridView中的单元格内显示图标和字符串

C# 在DataGridView中的单元格内显示图标和字符串,c#,datagridview,datagridviewcolumn,C#,Datagridview,Datagridviewcolumn,我想在DataGridView的一个单元格中显示两个变量 图标图标; 国际储备状态 我已经看过了,但我认为它很复杂,没有显示如何在一个单元格中显示变量 我不需要编辑,只需要显示两个变量 有人能给我举个小例子吗 我在C#4.0中工作,它是一个System.Windows.Forms.DataGridView以下是我自己的解决方案。只需将列类型设置为LagerStatusColumn,即可完成任务 public class LagerStatusColumn : DataGridViewColum

我想在DataGridView的一个单元格中显示两个变量

图标图标; 国际储备状态

我已经看过了,但我认为它很复杂,没有显示如何在一个单元格中显示变量

我不需要编辑,只需要显示两个变量

有人能给我举个小例子吗


我在C#4.0中工作,它是一个System.Windows.Forms.DataGridView

以下是我自己的解决方案。只需将列类型设置为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);
        }
    }
}
公共类LagerStatusColumn:DataGridViewColumn
{
公共LagerStatusColumn()
{
细胞模板=
新LagerStatusCell();
只读=真;
}
}
公共类LagerStatusCell:DataGridViewTextBoxCell
{
保护覆盖无效涂料(System.Drawing.Graphics Graphics、System.Drawing.Rectangle剪贴簿、System.Drawing.Rectangle cellBounds、int rowIndex、DataGridViewElementState cellState、对象值、对象格式化值、字符串错误文本、DataGridViewCellStyle cellStyle、DataGridViewAdvancedBorderStyle advancedBorderStyle、DataGridViewPaintParts paintParts)
{
绘制(图形、剪贴簿、单元格边界、行索引、单元格状态、值“”、错误文本、单元格样式、,
先进的边界样式、油漆部件);
var cellValue=Convert.IsDBNull(值)?0:Convert.ToDecimal(值);
常量int水平偏移=2;
var parent=(LagerStatusColumn)this.OwningColumn;
var fnt=parent.InheritedStyle.Font;
var icon=Properties.Resources.lager;
如果(cellValue==0)
icon=Properties.Resources.rest;
else if(单元格值<0)
icon=Properties.Resources.question\u白色;
常数int VERTFOSET=0;
graphics.DrawIcon(图标,cellBounds.X+水平偏移,
单元边界(Y+垂直偏移);
var cellText=formattedValue.ToString();
变量文本大小=
图形.测量(cellText,fnt);
//计算正确的颜色:
var textColor=parent.InheritedStyle.ForeColor;
如果((细胞状态&
DataGridViewElementState.Selected)==
DataGridViewElementState.Selected)
{
textColor=parent.InheritedStyle。
选择前景色;
}
//绘制文本:
使用(var笔刷=新的SolidBrush(textColor))
{
图形.抽绳(cellText、fnt、brush、,
cellBounds.X+图标宽度+2,
单元边界(Y+0);
}
}
}

您确定它是一个单元格吗?为什么它们不能在两个相邻的列中?它可以在两个单元格中,但可以做单元格间距。因为我只需要一个列标题。“库存”