.NET Programming
--
Questions
--
Followers
Top 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.
Dim myIC As New DataGridViewImageCell
'myIC.Value = Global.VPM3.My.Resources.R
'myIC.ImageLayout = DataGridViewImageCellLayou
'myPrcForm.dgPrcA.Rows(row
Thanks, bp
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
If myLRANGE = "4" Then
If myForm.dgPrcA.Columns(4).N
Dim cell As New DataGridViewImageCell
cell.Value = Global.VPM3.My.Resources.R
myForm.dgPrcA.Rows(0).Cell
End If
Any help at all with some sample code would be great.
Thanks, bp
Bob






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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
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

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.
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
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
Bob






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
This looks like you are using a web control which mine is using system.windows.forms.datag
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
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?

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.
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
Brenda
.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.