c# - DataGridView -同一行的图像和文本

时间:2022-09-13 09:35:24

I am looking for something like this on DataGridView:

我正在DataGridView上寻找这样的东西:

Image  | Text | 
Text   | Text |
Image  | Text

Basically, I just want Image cells and Text cells on the same row. I was able to get it to work with different types, for example: Checkbox, Text, and etc... But I am not able to get it to work with images.

基本上,我只想要图像单元格和文本单元格在同一行。我能够让它与不同的类型一起工作,例如:复选框、文本等等……但是我不能让它和图像一起工作。

I get this error: Invalid Cast from 'System.String' to 'System.Drawing.Image'

我得到这个错误:无效的铸造从'系统。字符串的“System.Drawing.Image”

Does anybody know the solution or have suggestion on how I should do this ? Thanks

有人知道解决办法吗?或者有什么建议吗?谢谢

3 个解决方案

#1


7  

It is relatively easy to have a column of type DataGridViewImageColumn display text in certain cells.

在某些单元格中,使用DataGridViewImageColumn类型的列显示文本相对容易。

All you need to do is replace a desired cell with a DataGridViewTextBoxCell.

您需要做的就是用DataGridViewTextBoxCell替换所需的单元格。

So for example, if I add the following image column to my grid:

例如,如果我将下面的图像列添加到网格中:

DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn .Name = "ImageColumn";
imageColumn .HeaderText = "An Image!";
Image i = Image.FromFile(@"C:\Pictures\TestPicture.jpg");
imageColumn.Image = i;
dataGridView1.Columns.Add(imageColumn);

You can replace a given cell with text like so (here in a button handler but you could also do it somewhere like within a databinding complete handler).

您可以将给定的单元格替换为如下所示的文本(这里是一个按钮处理程序,但是您也可以在类似于databinding complete处理程序的地方进行替换)。

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.Rows[3].Cells["ImageColumn"] = new DataGridViewTextBoxCell();
    dataGridView1.Rows[3].Cells["ImageColumn"].Value = "Some text!";  
}

This solution leaves a little bit of work for you if you want different images (you need to bind to a property of type image) and if you want different text. Since the Value property of an image column is of type Image you cannot use the same binding.

如果您想要不同的图像(需要绑定到类型为image的属性),或者想要不同的文本,这个解决方案会为您留下一些工作。由于图像列的值属性是类型图像,所以不能使用相同的绑定。

You could make your own overridden image column that handled this, but it would be a bit of work, that might not pay for itself vs. simply setting the value for the cells where you want text directly.

您可以创建自己的覆盖图像列来处理这个问题,但是这可能会有点麻烦,与直接为需要文本的单元格设置值相比,这可能不会带来任何代价。

#2


1  

I found I had to handle the DataGridViewCellFormattingEventHandler and assign the image in the DataGridViewCellFormattingEventHandler ( which was throwing the exception as it exited ).

我发现我必须处理DataGridViewCellFormattingEventHandler,并在DataGridViewCellFormattingEventHandler中分配映像(在它退出时抛出异常)。

Inside the DataGridViewCellFormattingEventHandler I assigned e->Value = System::Drawing::Image::FromFile("c:/Test.jpeg");
or
e.Value = System.Drawing.Image.FromFile("c:/Test.jpeg");

在DataGridViewCellFormattingEventHandler中,我分配e->值= System::: draw::::Image: FromFile(“c:/Test.jpeg”);或e。值= System.Drawing.Image.FromFile(“c:/ Test.jpeg”);

#3


0  

For better solution, Please refer this Blogspot post.

为了更好的解决方案,请参考这篇博文。

I tried the same solution for my project, it is working well, to display 16X16 pixels image in my datagrid cell, I have edited the function in above TextandImageColumn class:

我在我的项目中尝试了同样的解决方案,它运行良好,在我的datagrid单元中显示16X16像素的图像,我编辑了上面TextandImageColumn类的函数:

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)
    {
        // Paint the base content
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
           value, formattedValue, errorText, cellStyle,
           advancedBorderStyle, paintParts);

        if (this.Image != null)
        {
            PointF p = cellBounds.Location;
            p.X += 0;
            p.Y += 4;

            graphics.DrawImage(this.Image, p);
        }
    }

#1


7  

It is relatively easy to have a column of type DataGridViewImageColumn display text in certain cells.

在某些单元格中,使用DataGridViewImageColumn类型的列显示文本相对容易。

All you need to do is replace a desired cell with a DataGridViewTextBoxCell.

您需要做的就是用DataGridViewTextBoxCell替换所需的单元格。

So for example, if I add the following image column to my grid:

例如,如果我将下面的图像列添加到网格中:

DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn .Name = "ImageColumn";
imageColumn .HeaderText = "An Image!";
Image i = Image.FromFile(@"C:\Pictures\TestPicture.jpg");
imageColumn.Image = i;
dataGridView1.Columns.Add(imageColumn);

You can replace a given cell with text like so (here in a button handler but you could also do it somewhere like within a databinding complete handler).

您可以将给定的单元格替换为如下所示的文本(这里是一个按钮处理程序,但是您也可以在类似于databinding complete处理程序的地方进行替换)。

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.Rows[3].Cells["ImageColumn"] = new DataGridViewTextBoxCell();
    dataGridView1.Rows[3].Cells["ImageColumn"].Value = "Some text!";  
}

This solution leaves a little bit of work for you if you want different images (you need to bind to a property of type image) and if you want different text. Since the Value property of an image column is of type Image you cannot use the same binding.

如果您想要不同的图像(需要绑定到类型为image的属性),或者想要不同的文本,这个解决方案会为您留下一些工作。由于图像列的值属性是类型图像,所以不能使用相同的绑定。

You could make your own overridden image column that handled this, but it would be a bit of work, that might not pay for itself vs. simply setting the value for the cells where you want text directly.

您可以创建自己的覆盖图像列来处理这个问题,但是这可能会有点麻烦,与直接为需要文本的单元格设置值相比,这可能不会带来任何代价。

#2


1  

I found I had to handle the DataGridViewCellFormattingEventHandler and assign the image in the DataGridViewCellFormattingEventHandler ( which was throwing the exception as it exited ).

我发现我必须处理DataGridViewCellFormattingEventHandler,并在DataGridViewCellFormattingEventHandler中分配映像(在它退出时抛出异常)。

Inside the DataGridViewCellFormattingEventHandler I assigned e->Value = System::Drawing::Image::FromFile("c:/Test.jpeg");
or
e.Value = System.Drawing.Image.FromFile("c:/Test.jpeg");

在DataGridViewCellFormattingEventHandler中,我分配e->值= System::: draw::::Image: FromFile(“c:/Test.jpeg”);或e。值= System.Drawing.Image.FromFile(“c:/ Test.jpeg”);

#3


0  

For better solution, Please refer this Blogspot post.

为了更好的解决方案,请参考这篇博文。

I tried the same solution for my project, it is working well, to display 16X16 pixels image in my datagrid cell, I have edited the function in above TextandImageColumn class:

我在我的项目中尝试了同样的解决方案,它运行良好,在我的datagrid单元中显示16X16像素的图像,我编辑了上面TextandImageColumn类的函数:

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)
    {
        // Paint the base content
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
           value, formattedValue, errorText, cellStyle,
           advancedBorderStyle, paintParts);

        if (this.Image != null)
        {
            PointF p = cellBounds.Location;
            p.X += 0;
            p.Y += 4;

            graphics.DrawImage(this.Image, p);
        }
    }