BLOB binding to Image Column in DataGridView - What am I doing wrong? (C#)
Posted by: Florian
Date: January 09, 2007 09:55PM

Hi there,

I'm having a problem binding a BLOB column that contains images to an datagridview and I hope that someone can help me with the missing ingredient.

I am having a table with three columns, an auto-increment integer as key, a description as text and a BLOB that should contain small images. I have the table populated with entries with index and description but no image yet and this is why I want to have a datagridview bound to the table. One note up front: Changing the dataadapter to only return the two text only columns shows that the binding is correct here. I'm only having problems with the Image.

I go about as follows. First I get me myself my Dataadapter myadp built in a separate class and method and fill my Datasaet myds:

dvddbAccessHelper temp = new dvddbAccessHelper();
myadp = temp.ReturnRegionListDataAdapter();
myds = new DataSet();
myadp.Fill(myds);


Then I build my Datagridview and bind to the columns from the database:

dgvRegionView.Columns.Clear();

DataGridViewTextBoxCell newCell = new DataGridViewTextBoxCell();
DataGridViewColumn dc = new DataGridViewColumn(newCell);
dc.DataPropertyName = "idx_regions";
dgvRegionView.Columns.Add(dc);

DataGridViewColumn dc2 = new DataGridViewColumn(newCell);
dc2.DataPropertyName = "region_description";
dgvRegionView.Columns.Add(dc2);

DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.Width = 100;
imageColumn.DefaultCellStyle.NullValue = null;
imageColumn.ImageLayout = DataGridViewImageCellLayout.NotSet;
imageColumn.DataPropertyName = "region_image";
imageColumn.HeaderText = "Image";
dgvRegionView.Columns.Add(imageColumn);

dgvRegionView.CellFormatting += new
DataGridViewCellFormattingEventHandler(dgvRegionView_CellFormatting);
dgvRegionView.Columns[0].HeaderText = "Index";
dgvRegionView.Columns[1].HeaderText = "Titel";
dgvRegionView.AutoGenerateColumns = false;
dgvRegionView.DataSource = myds.Tables[0];

First problem when starting the dialog: Since there a currently null-values in the database for the BLOB I get DataError-Exceptions. I guess for now it's okay to just have an empy EventHandler so I don't see the automated Messagebox. But I do need the DataSource-Entry at the end, right?

Then I have an update-Method that is called upon the RowValidated and UserDeletedRow-Events:

private void UpdateData()
{
DataTable changes = myds.Tables[0].GetChanges();
if (changes != null)
{
myadp.Update(changes);
myds.Tables[0].AcceptChanges();
dgvRegionView.Update();
}
}

As I said: If I leave out the picture column in the dataadapter und datagridview, this works fine.

To get the Picture in the last column, I have an OpenFile-Dialog attached to the DoubleClick-Event and use the selected filename:

DataGridViewRow dgvr = dgvRegionView.CurrentRow;
String ImageFileName;
if (openFileDialogPicture.ShowDialog() == DialogResult.OK)
{
ImageFileName = openFileDialogPicture.FileName;
Image img = Image.FromFile(ImageFileName);
dgvRegionView.CurrentRow.Cells[2].Value = img;
int index = dgvRegionView.CurrentRow.Index;
this.Validate();
dgvRegionView.EndEdit();
UpdateData();
}

Now after this routine is hit, I see the picture in the last column of the current row. However the row is not marked "dirty" and therefore no changes are being sent back to the database. Obviously I am having a problem linking the last column with the dataset. But where is it? I'd really appreciate it if somebody can push my nose to the problem.

Thanks in advance!

Options: ReplyQuote


Subject
Written By
Posted
BLOB binding to Image Column in DataGridView - What am I doing wrong? (C#)
January 09, 2007 09:55PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.