Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Byte[]'.

$
0
0
Hello,
 
I am using this code for image display in picturebox from datagridview cell click event but it's getting error:-
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
 
private void dgvDisplayTiles_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
//Get image data from gridview column.
txtTileName.Text = dgvDisplayTiles.CurrentRow.Cells[2].Value.ToString();
cmbtilecategory.Text = dgvDisplayTiles.CurrentRow.Cells[1].Value.ToString();
lblTileID.Text = dgvDisplayTiles.CurrentRow.Cells[0].Value.ToString();
byte[] imageData = (byte[])dgvDisplayTiles.CurrentRow.Cells[3].Value;//Error
i = e.RowIndex;
//Initialize image variable
Image newImage;
//Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
 
//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}
 
//set picture for preview
picTextureDisplay1.Image = newImage;
 
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}}
 
Please help me.
 
Thanks in Advance.
 
Ankit Agarwal
Software Engineer

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>