ImageColumn Binding In DataGridView

Hi everyone,
I need bind a image from a dataset to a picturebox.
i found the code in KB but its in Visual C#

DataSet ds = new DataSet();
da.Fill(ds, "BLOBTest");
int c = ds.Tables["BLOBTest"].Rows.Count;

//BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.

Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);

I would greatly appreciate if anyone can translate it i n VB. i have done so far and getting error:

dim byteBLOBData() as byte
byteBLOBData() = ds.Tables("BLOBTest").Rows(c - 1)("BLOBData") -- getting error here

any help appreciated

[806 byte] By [Arvin] at [2007-12-16]
# 1

Based on what you have above, I'd suggest the following:


Dim img As Byte()
img = CType(ds.Tables("BLOBTest").Rows(0)("BLOBData"), Byte())
Me.PictureBox1.Image = Image.FromStream(New System.IO.MemoryStream(img))

Personally, I'd use DataBinding. To do this, you first need to setup a Binding - you can do this in the designer or in the Form_Load event as follows:

Declare a variable:


Dim WithEvents bind As Binding

Create the binding in Form_Load:


bind = New Binding("Image", Me.ds, "BLOBTest.BLOBData")
Me.PictureBox1.DataBindings.Add(bind)

And then add a Parse event:


Private Sub Binding_Format(ByVal sender As System.Object, ByVal e As ConvertEventArgs) Handles bind.Format
Dim img As Byte()

img = CType(e.Value, Byte())

If (Not img Is Nothing) Then
e.Value = Image.FromStream(New System.IO.MemoryStream(img))
End If
End Sub


Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Dear all,

If i want to binding ImageColumn in DataGridView, I always get error formating in DataError event of DataGridView. Specially, Data Type in Database is Bit type or String type.

//Function create DataGridViewImageColumn
private
DataGridViewImageColumn GetImageColumn(string headerText, string dataPropertyName, int width)
{
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.HeaderText = headerText;
imageColumn.DataPropertyName = dataPropertyName;
imageColumn.Name = dataPropertyName;
imageColumn.Width = width;
return imageColumn;
}

//Add imageColumn in DataGridView
grdStandardNotes.Columns.Add(GetImageColumn(
"Default", "Default", 50));

//Data Error Event
private void grdStandardNotes_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
try
{
anError.Cancel =
true;
MessageBox.Show("Error happened " + anError.Context.ToString());
if anError.Context == DataGridViewDataErrorContexts.Formatting || anError.Context == DataGridViewDataErrorContexts.Display)
{
MessageBox.Show("Commit error");
}
if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
{
MessageBox.Show("Cell change");
}
if (anError.Context == DataGridViewDataErrorContexts.Parsing)
{
MessageBox.Show("parsing error");
}
if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
{
MessageBox.Show("leave control error");
}
if ((anError.Exception) is ConstraintException)
{
DataGridView view = (DataGridView)sender;
view.Rows[anError.RowIndex].ErrorText =
"an error";
view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText =
"an error";
anError.ThrowException =
false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Please help me fix my code. Thanks for your cooperation.

khiemvo at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...