C # - DataGridView - image and text on one line

I am looking for something like this in a DataGridView:

Image | Text | Text | Text | Image | Text 

Basically, I just want the cells Cells and text cells to be on the same line. I was able to get it to work with different types, for example: Checkbox, Text, etc. .... But I can not get it to work with images.

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

Does anyone know a solution or have a suggestion on how I should do this? Thanks

+6
source share
3 answers

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

All you have to do is replace the desired DataGridViewTextBoxCell cell.

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 this cell with such text (here in the button handler, but you can also do it somewhere, like in the full data binding handler).

 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 work for you if you need different images (you need to bind to a property like image), and if you need different text. Since the Value property of an image column is of type Image, you cannot use the same binding.

You can create your own column with an overridden image that would handle this, but that would be a bit of work that might not pay for itself, but simply set the value for cells where you want the text directly.

+7
source

I found that I had to process the DataGridViewCellFormattingEventHandler and assign the image to the DataGridViewCellFormattingEventHandler (which threw an exception when it was thrown).

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

+1
source

For a better solution, please refer to Blogspot .

I tried the same solution for my project, it works well to display a 16X16 pixel image in my datagrid cell, I edited the function in the TextandImageColumn class:

 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; pX += 0; pY += 4; graphics.DrawImage(this.Image, p); } } 
0
source

Source: https://habr.com/ru/post/920472/


All Articles