?
Solved

vb.net 2008 datagridviewimagecolumn not showing images in my datagridview

Posted on 2012-09-11
8
Medium Priority
?
1,494 Views
Last Modified: 2012-09-13
I have a windows form and it has a datagridview on it. In my code I greate columns and then populate the datagridview from a database. One of the columns is a datagridviewimagecolumn and it is pulling from an image in the database (actual image not path/filename).

I populate the datagridview by loading up a sqldatareader (dr in this case) then I do this...
dt = New DataTable
dt.Load(dr)
dgv.DataSource = dt

Open in new window


The datagridview loads perfectly EXCEPT for the image column cells. NOTHING is shown.

When I run the debugger the column is there, but it cells defaultstyle claims they are not visible. In addition the image information is correct, the cells in the debugger show the right length and width so I can tell they are sized right.

Any ideas?
0
Comment
Question by:rhawk
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38390024
Why use a DataReader when you want a DataTable?

What is the type of image column in DB?

Try

Dim dbadp As New SqlDataAdapter("Select ...", "connection string")
Dim dTable As New DataTable
dbadp.Fill(dTable)
dbadp.Dispose
0
 
LVL 2

Author Comment

by:rhawk
ID: 38390993
Why does it matter if I use the reader or not? (asking if it makes a difference to the images working only)
The data is coming from a view. THe image column fulls from a table that uses an image column.

OKay, I adjusted the code to this:
Dim SqlQry As String = GetGridSQL(True)
Dim adp As New SqlDataAdapter(SqlQry, AdodcListDockets)
dt = New DataTable
adp.Fill(dt)
dgv.DataSource = dt

Open in new window


The getgridsql just builds the sql needed. This new code made no difference. It seems to work just like the old code and did not make the image column have the images. Since it made no difference I am going to go back to the old load routine since it is used in 9 other forms and I rather they all remaint he same.

Any other ideas?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38391231
Datareader is used for row by row sequential access of data usually on a details form. For showing rows in a grid, datatable with adapter is the way to go. You can keep using your old approach but if I were you, I would change other code as well to look like this.

I use varbinary instead of image in SQL Server and my images show fine.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:rhawk
ID: 38391278
On the reader, I did not make the original code and just want to change only what I need to. :/

2 of the screens do show images and work, so i cannot figure out why this one (after I added the column) wont. :(
0
 
LVL 2

Author Comment

by:rhawk
ID: 38391314
CodeCruiser, here is something that may allow you to solve this for me a different way. I had tried adding a cellformatting and then cellpainting event on the datagridview and in it I checked a hidden cell value to decide what image to show, and I simply assigned the image to the imagecolumn (it is a single image or NO image only) and it worked, sort of. The issue was the image column was flickering like it was going through the event over and over and over. If you can explain how to do it through one of those events and not flicker/shimmer then that will be a fast fix and get me out of this horrible code. Any idea?

This is the code I tried that flickered...
If e.RowIndex > -1 Then
	If TypeOf dgv.Columns(e.ColumnIndex) Is DataGridViewImageColumn Then
		If dgv.Rows(e.RowIndex).Cells("SpecialID").Value IsNot DBNull.Value Then
			DirectCast(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewImageCell).Value = ImageList1.Images.Item(2)
		End If
	End If
End If

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 750 total points
ID: 38395226
Instead of assigning the image again and again in CellFormatting event, you can add it once at the start. Check example here

http://niitdeveloper.blogspot.co.uk/2010/07/display-image-instead-of-checkbox-in.html
0
 
LVL 2

Author Comment

by:rhawk
ID: 38395350
You are saying to walk the grid each time it is loaded and set the image then?
I suppose I can do that. The grid can and will be repopulated many times as the user sets search information and refreshes it as needed and conrols how many rows it will display.
I'll adjust the load routine later today to assign the image and see how that works.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38395636
That would still be better than CellFormatting as CellFormatting probably executes loads of times as you scroll, mouseover, display/hide the grid.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question