Question download a selected row from DataGridView?

moracom

Member
Joined
Aug 15, 2008
Messages
5
Programming Experience
5-10
I have this windows form and in it i have datagridview that displays the records from a table in a database and it works just fine but I cant figure out how to allow the user to download a selected row from the datagridview.
Any help is greatly appreciated.
 
Each row consists of two columns. Column one contains the id number and column two contains an image. Yes you've read correctly and image is being stored in the database as blob (client's choice). Mainly I just want to be able to select an image and download it onto my hard drive. How do i go about doing this?
 
VB.NET:
CType(Me.DataGridView1.CurrentRow.Cells("ImageColumn").Value, Image).Save("image.bmp")
 
This should go in whatever event is your "save" action.

If the user selects a row on the grid, and then clicks a button to download that image, this code would go in the click event of that button.
 
i have setup two new columns
1. an unbound DataGridViewButtonColumn called DownloadImg but i dont know where to place your line of code.
2. a bound DataGridViewButtonColumn with DataPropertyName as IDNumber and again i dont know where to place the line of code
 
As decribed in DataGridViewButtonColumn help:
To respond to user button clicks, handle the DataGridView.CellContentClick event.
This is the default event for DataGridView control, so doubleclicking it brings you to the handler in code where you add your code. Use the provided e.ColumnIndex to see if it was a cell in button column that was clicked.
CType(Me.DataGridView1.CurrentRow.Cells("ImageColumn").Value, Image).Save("image.bmp")
That would work with unbound image cell, but I forgot that values in image cells bound to database is actually byte arrays, use this instead:
VB.NET:
My.Computer.FileSystem.WriteAllBytes("image.bmp", Me.DataGridView1.CurrentRow.Cells("ImageColumn").Value, False)
 
This is what i have right now but it doesnt seem to be doing anything:
VB.NET:
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
[B]        My.Computer.FileSystem.WriteAllBytes("ImgPictureDataGridViewImageColumn.jpg", Me.DataGridView1.CurrentRow.Cells("ImgPictureDataGridViewImageColumn").Value, False)[/B]
    End Sub
 
This is what i have right now but it doesnt seem to be doing anything:
It should have, it works fine when I run it, I can't think of any reason it wouldn't work for you.
 
Back
Top