VB.NET Datagrid with pictures

M

Matt

Guys I could really use some help with this. I think that it's
probably a simple solution but I haven't been able to find anything.

I have a datagrid on my form that I populate via code like this...

Me.My_DataGrid.Rows.Add(Column1, Column2, Column3)

Well now I have a datagrid that I want to populate with an image.

So for example...

Me.My_DataGrid.Rows.Add(Column1, Column2, Picture)

How do I do this?

I know I can change the "column type" to an "image" column but how do I
add one with code?

The next best thing would to be able to statically add an image and
make it visible or invisible by row.

Any advice would be appreciated.
 
S

Scott M.

I've done this by placing an image control in a template column of a
DataGrid and then binding the image control's url property to the path of
the image (which is stored in my database along with the other data for that
given record).

Works like a charm.
 
M

Matt

I'm not sure I understand how to do this scott, could you elaborate a
little bit please?

Thank you :)
 
S

Scott M.

You need to add "Template" columns to your DataGrid first....

1. Right click on your grid and choose "Property Builder"
2. Select the "Columns" category on the left side
3. Select a "Bound Column" from the "Available Columns" section
4. Click the ">" button to add a bound column to the "Selected Columns"
section
5. Enter the field name from your data source you want to bind to in the
"DataField" textbox.
(this should be the name of the field in your database that contains
the image path)
6. Now, click the hyperlink at the bottom of the window that says "Convert
this column into a Template column"
7. Type what you want this column to have as header text in the "Header
Text" textbox
8. Click OK
9. Right click on your grid and choose "Edit Template" and when the sub
menu appears choose the new column you just added.
10. There will already be a label control in the "Item Template" section of
the screen you see. Select it and look at its "DataBindings" property
(you'll need to click the "..." button to get there.
11. Select the whole "Custom Binding Exression" text & click "Cancel"
12. Now, delete the label control and replace it with an Image control.
13. Go into the DataBindings property and paste in the same text you just
copied out of the label.

Assuming the rest of your DataGrid's code is set up to bind to its data
source, you should now have an image in your grid.
 
M

Miro

Thank you scott,

I was reading the posts and I seen this one and wondered how to do this.

I would have also had to ask this about a month later.
Thank you for the step by step example.

Miro
 
M

Matt

Scott,

Although I truly appreciate the effort I believe that your instructions
are refering to a datagrid in ASP.NET, the type that I'm refering to is
for VB.NET on a form.
 
S

Scott M.

Yes, you are right Matt. Sorry, I didn't realize you wanted a Windows Forms
answer. Honestly, I don't do much work in WinForms, so I really can't offer
a suggestion on this.

Sorry!

Good luck.
 
M

Matt

I actually figured it out last night. You have to create an Image
object and pass it in like you would a string.

But thanks to everyone for your efforts!

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top