DataGridView and image cell problem

F

Fencer

Hello, I'm having problems with DataGridView I need help with. It's the
first time I use this control (and I'm struggling to wrap my head around
it) and my C# skills are rather rusty.

Anyway, I created the DataGridView like this:

grid.ColumnCount = 4;
grid.Dock = DockStyle.Fill;
grid.Parent = this;
grid.AutoSize = true;
grid.BorderStyle = BorderStyle.Fixed3D;

grid.Columns.Clear();
grid.Columns.Add(new DataGridViewImageColumn());
grid.Columns.Add(new DataGridViewImageColumn());
grid.Columns.Add(new DataGridViewImageColumn());
grid.Columns.Add(new DataGridViewImageColumn());

then I add my Rows like this:
grid.Rows.Add(new Tile[] { new Tile(), new Tile(), new Tile(), new
Tile() });
Side note: Here I actually wanted to get a row from my 2D-array of Tiles
but I couldn't make it work

Tile inherits from DataGridViewImageCell and in the constructor I set
the property "Value" to a bitmap I've loaded and that's basically it.
However, something's missing because when I run the program I get a long
string of error dialogs complaining (and here I have to translate from
my localized version):
The following exception occured in DataGridView:
System.FormatException: Cannot convert the value DataGridViewImageCell {
ColumnIndex = -1, RowIndex = -1} to the type Image...

I suspect I may need to do more things on the grid and in my Tile class,
but I'm having serious problems figuring exactly what, so I ask for the
assistance of the this group.

Thanks in advance for any help!

- Fencer
 
F

Fencer

[snip my OP]

Since I didn't receive any replies I've decided to write a short test
program that illustrates the problem I described in OP:

using System;
using System.Drawing;
using System.Windows.Forms;

class Program : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}

Program()
{
Width = 800;
Height = 600;

grid.Dock = DockStyle.Fill;
grid.Parent = this;
grid.AutoSize = true;

grid.Columns.Add(new DataGridViewImageColumn());

grid.Rows.Add(new MyTile[] {new MyTile()});

Controls.Add(grid);
}

private DataGridView grid = new DataGridView();
}

class MyTile : DataGridViewImageCell
{
public MyTile()
{
Value = new Bitmap("../../start.bmp");
}
}

Notice that you need a bitmap "start.bmp" in the project root. Also, if
I change the column type to DataGridViewTextBoxColumn for example, the
program doesn't throw the exception I described in my OP.

- Fencer
 
F

Family Tree Mike

Fencer said:
[snip my OP]

Since I didn't receive any replies I've decided to write a short test
program that illustrates the problem I described in OP:

using System;
using System.Drawing;
using System.Windows.Forms;

class Program : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}

Program()
{
Width = 800;
Height = 600;

grid.Dock = DockStyle.Fill;
grid.Parent = this;
grid.AutoSize = true;

grid.Columns.Add(new DataGridViewImageColumn());

grid.Rows.Add(new MyTile[] {new MyTile()});

Controls.Add(grid);
}

private DataGridView grid = new DataGridView();
}

class MyTile : DataGridViewImageCell
{
public MyTile()
{
Value = new Bitmap("../../start.bmp");
}
}

Notice that you need a bitmap "start.bmp" in the project root. Also, if
I change the column type to DataGridViewTextBoxColumn for example, the
program doesn't throw the exception I described in my OP.

- Fencer

You should be adding Image objects, such as a Bitmap made from that
"start.bmp", not a DataGridViewImageCell. When you add a row to your
DataGridView, you have told it already that the column will be a
DataGridViewImageColumn, so you basically you are putting an ImageCell
inside the ImageCell it creates by default.
 
F

Fencer

Family said:
Fencer said:
[snip my OP]

Since I didn't receive any replies I've decided to write a short test
program that illustrates the problem I described in OP:

using System;
using System.Drawing;
using System.Windows.Forms;

class Program : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}

Program()
{
Width = 800;
Height = 600;

grid.Dock = DockStyle.Fill;
grid.Parent = this;
grid.AutoSize = true;

grid.Columns.Add(new DataGridViewImageColumn());

grid.Rows.Add(new MyTile[] {new MyTile()});

Controls.Add(grid);
}

private DataGridView grid = new DataGridView();
}

class MyTile : DataGridViewImageCell
{
public MyTile()
{
Value = new Bitmap("../../start.bmp");
}
}

Notice that you need a bitmap "start.bmp" in the project root. Also,
if I change the column type to DataGridViewTextBoxColumn for example,
the program doesn't throw the exception I described in my OP.

- Fencer

You should be adding Image objects, such as a Bitmap made from that
"start.bmp", not a DataGridViewImageCell. When you add a row to your
DataGridView, you have told it already that the column will be a
DataGridViewImageColumn, so you basically you are putting an ImageCell
inside the ImageCell it creates by default.

Thanks, that works. I can see the image fine. However, I was planning on
using the tile class for containing the logic which image should be
displayed. Where would that go now? I actually want to do a simulation
where the visual output is a grid of images (different) that will be
changed as the simulation progresses.

- Fencer
 
F

Fencer

Fencer said:
Thanks, that works. I can see the image fine. However, I was planning on
using the tile class for containing the logic which image should be
displayed. Where would that go now? I actually want to do a simulation
where the visual output is a grid of images (different) that will be
changed as the simulation progresses.

- Fencer

I think I will go about it in an MVC-fashion. So I will run my
simulation in a model that doesn't know about the datagridview or the
form and it will speak to a controller that will update the
(datagrid)view as the simulation progresses.

- Fencer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top