We help IT Professionals succeed at work.

I have a datagridview that I need to display graphics within in vb.net 2005, 500 points

systems_ax
systems_ax asked
on
644 Views
Last Modified: 2013-11-26
I have a datargid that populates depending on a search criteria.  I would like to know if it is possible at all to display a graphic next to the text or within the same column as text.  I am developing in vb.net 2005.  Please, advise as I cannot find any information on such a topic.  I have an access database 2003 serving as a backend.
For instance: I search for Mark an dthe datagrid shows a graphic next to mark for each record that it pulls.
Please, advise if this is possible.
Comment
Watch Question

Commented:
here you find some samples for what you need:
http://www.codeproject.com/cs/miscctrl/csharpgridcontrol.asp

if you only need to set an image in a cell depending on the values present in the other row cells you can handle the CellFormatting event changing the image cell value in real time



        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView d = (DataGridView)sender;
            if (e.RowIndex > -1)
            {
                if (e.ColumnIndex == 0)//index for the image column
                {
                    DataGridViewRow row = d.Rows[e.RowIndex];
                    if (row.Cells["name"].Value.ToString().Contains("Mark"))
                    {
                        row.Cells[0].Value = image1;
                    }
                }
            }
        }

you can also draw an image in the same cell. in this case you should handle the CellPainting event drawing your image AND the text you need since the datagrid control do not handle the painting of the cell

Author

Commented:
nogovoia,
 thank you, I will let you know how it went

Author

Commented:
nogovoia,
unfortunately I do not know C sharp language.  I am developing with visual basic.net 2005.
any other helpful links?

Commented:
here's the above code snippet translated in vb.net

    Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        Dim d As DataGridView = sender
        If e.RowIndex > -1 Then
            If e.ColumnIndex = 0 Then
                Dim row As DataGridViewRow = d.Rows(e.RowIndex)
                If row.Cells("name").Value.ToString().Contains("Mark") Then
                    row.Cells(0).Value = Image1 ' or whatever you want
                End If
            End If
        End If
    End Sub

sadly on codeproject there are much more c# code samples than vb.net so... start learning c#!!! =D

still looking if I find something more comprehensive in vb.net

Author

Commented:
nogovoia,
thank you very much, I am still learning visual basic so when I do know it pretty well, then I will take a look at c#. :)
thank you for your sample.

Author

Commented:
nogovoia,
I modified your code slightly, but I am doign something wrong because no images display.  

Private Sub dgvLastNames_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvLastNames.CellFormatting
        Dim d As DataGridView = sender
        If e.RowIndex > -1 Then
            If e.ColumnIndex = 0 Then
                Dim row As DataGridViewRow = d.Rows(e.RowIndex)
                If row.Cells("Reports").Value.ToString().Contains("Reports") Then
                    row.Cells(0).Value = row.Cells("world_splash.jpg").Value ' added this
                End If
            End If
        End If
    End Sub

any ideas?
thank you

Author

Commented:
it was giving me a message that "Image1" is not declared so I had to add "row.Cells("world_splash.jpg").Value"

what am I doing wrong?
thank you

Commented:
first of all, did you declare the column as datagridimagecolumn?

then go to the project properties, resources tab, choose add existing file. browse to your image location an add it to the resources. this is a better way to bind your image with the project and deploy it embedded in the exe file. now you can access this image typing Properties.Resources.ImageName (not the file name, the resource name, you'll find it through intellisense).

now you can set the imagecellvalue to row.Cells(0).Value = Properties.Resources.ImageName

Author

Commented:
nogovoia,
I am having a really hard time with this code.  Are you referring to the column within Access database, when you say "declare the column as datagridimagecolumn"?  I did go to properties and added a file named image_splash which is a jpg.  

row.Cells(0).Value = Properties.Resources.ImageName ***on this line,it says properties is not declared.
I am working on this and will let you know of my progress.
thank you very much

Commented:
read this tutorial on using resources with vb.net

http://visualbasic.about.com/od/usingvbnet/a/ResVBNET.htm

when i was talking about declaring the first column as an image column i was referring to the datagridview control. you can bind this control to datasources in several ways. the one I'm referring to is to create a datatable with the data you want to see and bind it to the datagridview through the datasource property (if you want to implement filtering and sorting capabilities later set a bindingsource in the middle). then you can add how much columns you want using the ide editor and set their relative names and datapropertynames (which allow you to bind the field to the related datatable field)

try to get the sense of the code below

///

Imports System.Data
Imports System.Data.OleDb

Public Class Form2

    Private conn As OleDbConnection
    Private connectionString As String

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        conn = New OleDbConnection(connectionString)
        conn.Open()

        Dim sql As String = "select a,b,c from table"
        Dim cmd As New OleDbCommand(sql, conn)
        Dim da As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)

        Me.DataGridView1.Columns.Add(New DataGridViewImageColumn())
        Me.DataGridView1.Columns(0).Name = "imageColumn"
        Me.DataGridView1.Columns.Add(New DataGridViewImageColumn())
        Me.DataGridView1.Columns(1).Name = "fieldA"
        Me.DataGridView1.Columns(1).DataPropertyName = "a"
        Me.DataGridView1.Columns.Add(New DataGridViewImageColumn())
        Me.DataGridView1.Columns(2).Name = "fieldB"
        Me.DataGridView1.Columns(2).DataPropertyName = "b"
        Me.DataGridView1.Columns.Add(New DataGridViewImageColumn())
        Me.DataGridView1.Columns(3).Name = "fieldC"
        Me.DataGridView1.Columns(3).DataPropertyName = "c"

        Me.BindingSource1.DataSource = dt
        Me.BindingSource1.Sort = "b,a,c"
        Me.DataGridView1.DataSource = Me.BindingSource1

        conn.Close()

    End Sub
End Class

Author

Commented:
nogovoia,
this is my current code that searches records:so changing this code is not possible.  Should I just add an extra column to the Access Database table?
 Private Sub Populate_Information(ByVal LastName As String)
              dgvLastNames.Columns.Clear()
        OpenDB()
        LastName = "'%" & LastName & "%'"

        If ComboBox1.Text = "" Then
            MessageBox.Show("Please Enter the Last Name!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else

            Dim Sql As String = "SELECT FirstName, MiddleName FROM Employees Where LastName LIKE " & LastName
            Dim cmd As New OleDbCommand(Sql, Conn)
            Dim da As New OleDbDataAdapter
            Dim ds As New DataSet
            Dim dt As DataTable
            da.SelectCommand = cmd
            Try
                da.Fill(ds, "Data")
                dt = ds.Tables("Data")
                dgvLastNames.AutoGenerateColumns = False
                Dim x As Integer
                For x = 0 To dt.Columns.Count - 1
                    Dim dgc As New DataGridViewTextBoxColumn
                    With dgc
                        .Name = dt.Columns(x).ColumnName.ToString
                        .DataPropertyName = dt.Columns(x).ColumnName.ToString
                        .HeaderText = dt.Columns(x).ColumnName.ToString
                        .Width = 100
                    End With
                    dgvLastNames.Columns.Add(dgc)
                Next
                dgvLastNames.DataSource = dt


                For Each dgrow As DataGridViewRow In dgvLastNames.Rows
                    If Not IsDBNull(dgrow.Cells("Employees").Value) AndAlso dgrow.Cells("Employees").Value IsNot Nothing Then
                        dgrow.Cells("Employees").Value = dgrow.Cells("Employees").Value.ToString.Replace("d:\", "").Replace("D:\", "")
                    End If
                Next

                             If x <= 0 Then
                    MessageBox.Show("No Record Found!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Catch ex As Exception
                MsgBox("Error while executing: (" & cmd.CommandText & ")" & vbCrLf & "Msg: " & ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
            Finally
                da = Nothing
                ds = Nothing
                dt = Nothing
                cmd.Dispose()
                CloseDB()
            End Try
        End If
    End Sub

Thank you very much for the tutorial, but I am still having troubles with this issue.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
nogovoia,
still no success.
I am going to go through this entire post again and see what i am doing wrong.

Author

Commented:
nogovoia,
i give up, this is too confusing.  Is there any way to display 2 columns under 1 column within the datagridview.  I am trying to take 2 fcolumns, word document and graphics columns and putting them under 1 column within the data grid.
Is this possible?

Author

Commented:
can you extend it for a few more weeks please?

Author

Commented:
a few more weeks please.

Commented:
I think the problem that systems ax is having here is that the Cell that he wants the Image to go in, is not defined as an Image CellType.
This is my problem in a post earlier today.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.