Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of Bpangle
Bpangle

Add an image to a datagridview cell programmatically when it meets a certain condition
Hello Experts,
I have a datagridview on a windows form and am trying to add a "dot" image to a column in the grid based on a bound datasource. I am trying to do this in vb.net.  

 When the data returned in column 1 is greater than or equal to the number 4 then I want the image that appears in column 5 to appear and if it is less than 2 I do not want to display an image.  If I set that cell to an image cell it is returning an image no matter what criteria it hits so I tried setting the cell to a text cell and now I'm not getting any image.  Your help is greatly appreciated!


Thanks, bp

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of BpangleBpangle

ASKER

I have tried setting the column to a text box and then declaring a new instance of the cell to an image cell and at runtime this does not display an image if it meets the criteria.  Any suggestions?

Dim myIC As New DataGridViewImageCell

               
                'myIC.Value = Global.VPM3.My.Resources.RedDotW
                'myIC.ImageLayout = DataGridViewImageCellLayout.Normal
                'myPrcForm.dgPrcA.Rows(row.Index).Cells.Item("BCNS") = myIC

Thanks, bp

Avatar of BpangleBpangle

ASKER

Experts -
Listed here is my last try at trying to display an image in a cell based on the criteria

 Dim myLRANGE As String = Mid(row.Cells.Item("LRANGE").Value, 1, 1)


                          If myLRANGE = "4" Then


                         If myForm.dgPrcA.Columns(4).Name = "BCNS" Then
                        Dim cell As New DataGridViewImageCell
                        cell.Value = Global.VPM3.My.Resources.RedDotW
                        myForm.dgPrcA.Rows(0).Cells("BCNS").Value = cell
                                  End If

Any help at all with some sample code would be great.
Thanks, bp

Avatar of Bob LearnedBob Learned🇺🇸

You could use the CellPainting event handler to draw an image based on the value from another cell.

Bob

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of BpangleBpangle

ASKER

Bob - Could you give me a sample of how you would do that?

Avatar of Bob LearnedBob Learned🇺🇸

Here is my example, which highlights a few things that you can do with the DataGridView, not just painting the cell.

Bob
Imports System.Collections.Generic
Imports System.IO
 
Public Class DataGridViewExampleForm
 
  Private m_FileList As List(Of String)
 
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.ConfigureGrid()
    Me.LoadFiles()
    Me.DisplayFiles()
  End Sub
 
  Private Sub ConfigureGrid()
    Me.gridExecutables.AllowUserToAddRows = False
    Me.gridExecutables.AllowUserToDeleteRows = False
    Me.gridExecutables.AllowUserToOrderColumns = False
    Me.gridExecutables.AllowUserToResizeColumns = False
    Me.gridExecutables.AllowUserToResizeRows = False
    Me.gridExecutables.AlternatingRowsDefaultCellStyle.BackColor = Color.LightSteelBlue
    Me.gridExecutables.AutoGenerateColumns = False
    Me.gridExecutables.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    Me.gridExecutables.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
    Me.gridExecutables.CellBorderStyle = DataGridViewCellBorderStyle.Raised
    Me.gridExecutables.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
    Me.gridExecutables.Dock = DockStyle.Fill
    Me.gridExecutables.EditMode = DataGridViewEditMode.EditProgrammatically
    Me.gridExecutables.Font = New Font("Tahoma", 8)
    Me.gridExecutables.ColumnHeadersDefaultCellStyle.Font = New Font(Me.gridExecutables.Font, FontStyle.Bold)
    Me.gridExecutables.GridColor = Color.SteelBlue
    Me.gridExecutables.RowHeadersVisible = False
    Me.gridExecutables.RowTemplate.Height = 40
    Me.gridExecutables.ScrollBars = ScrollBars.Vertical
    Me.gridExecutables.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    Me.gridExecutables.ShowEditingIcon = False
    Me.gridExecutables.ShowRowErrors = False
    Me.gridExecutables.VirtualMode = True
 
    AddHandler Me.gridExecutables.CellPainting, AddressOf PaintCell
    AddHandler Me.gridExecutables.CellValueNeeded, AddressOf SetCellValue
  End Sub
 
  Private Sub LoadFiles()
    m_FileList = New List(Of String)
    m_FileList.AddRange(My.Computer.FileSystem.GetFiles("C:\Windows", FileIO.SearchOption.SearchTopLevelOnly, "*.exe"))
  End Sub
 
  Private Sub DisplayFiles()
    Me.gridExecutables.ColumnCount = 2
    Me.gridExecutables.Columns(0).HeaderText = "File Name"
    Me.gridExecutables.Columns(0).SortMode = DataGridViewColumnSortMode.Automatic
    Me.gridExecutables.Columns(1).HeaderText = "Associated Icon"
    Me.gridExecutables.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
    Me.gridExecutables.Columns(1).FillWeight = 25
    Me.gridExecutables.RowCount = m_FileList.Count
  End Sub
 
  Private Sub PaintCell(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
    If e.RowIndex > -1 AndAlso e.ColumnIndex = 1 Then
      Dim fileName As String = Me.gridExecutables(0, e.RowIndex).Value
      Dim ico As Icon = Icon.ExtractAssociatedIcon(fileName)
      e.PaintBackground(e.ClipBounds, e.State = DataGridViewElementStates.Selected)
 
      Dim x As Integer = e.CellBounds.Left + ((e.CellBounds.Width - ico.Width) \ 2)
      e.Graphics.DrawIcon(ico, x, e.CellBounds.Top)
      e.Handled = True
    End If
  End Sub
 
  Private Sub SetCellValue(ByVal sender As Object, ByVal e As DataGridViewCellValueEventArgs)
    If e.RowIndex > -1 AndAlso e.ColumnIndex = 0 Then
      e.Value = m_FileList(e.RowIndex)
    End If
  End Sub
 
End Class

Open in new window

DataGridView-Example.png

Avatar of BpangleBpangle

ASKER

Bob,
Is the code you gave me available in vb.net for windows form applications?  I am not finding some of those events.  I am working on modifying it to see if I can get it to work in my application by changing up some of the events.
Thanks, Brenda

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Bob LearnedBob Learned🇺🇸

Brenda,

That is a Windows Form example for the 2.0 DataGridView.  If you are having difficulties mapping the example to the way that you need to use it, please let me know how you are trying to use it.  More explicitly, where are you trying to find the events?

Bob

Avatar of BpangleBpangle

ASKER

Bob,
I'm trying to find the events on the datagrid itself.  I also looked on the form and didn't see them there either.  I'm sure it has something to do with my inexperience!  Thanks, Brenda

Avatar of Bob LearnedBob Learned🇺🇸

Here is a screen shot of using the Events button on the Property Grid to show the events for a control.

Bob
-SqlDataSource-Events.png

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of BpangleBpangle

ASKER

Bob,
This looks like you are using a web control which mine is using  system.windows.forms.datagridview and I do not have any of the events you show in the property grid above.
Brenda

Avatar of Bob LearnedBob Learned🇺🇸

Brenda,

That was only an example of showing the events for control.  The details for the events will be different depending on which control type you have selected.  The steps are the same:

1) Click the Events button on the property grid (lightning bolt)
2) Select the control on the form
3) Look through the list of events
4) Double-click on the event to add a default event handler

Bob

Avatar of BpangleBpangle

ASKER

Bob,
Ok Sorry thought you were sending me the propery grid shot of what mine should look like.  I have looked through my entire list of events and neither of the two events you mentioned above live there.  I have CellPainting but not PaintCell I do however have Paint.  I'm attaching a screen shot of my property grid so you can see what I have available.  Maybe I'm just not running an updated version?
dgtest.bmp

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Bob LearnedBob Learned🇺🇸

Brenda,

What I called PaintCell is the event handler for what you called dgPrcA_Click.  My example code was meant to show the realm of possibilities, but apparently has only served to confuse you a little.   What you see in PaintCell would go in the event handler stub that was created when you double-clicked on CellPainting.

Bob

Avatar of BpangleBpangle

ASKER

Oh and I am so easily confused when it comes to this!!  Thanks for clearing that up I'll continue to try and work through this and will let you know tomorrow what I come up with.  I appreciate your patience.
Brenda

ASKER CERTIFIED SOLUTION
Avatar of BpangleBpangle

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.