Results 1 to 4 of 4

Thread: Click DataGridView cell, change its background image

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    47

    Post Click DataGridView cell, change its background image

    Hello,

    I'm trying to make a visual map editor, and figured that a DataGridView would be the best way to go. I would like to make it so that you can click one of many buttons to set which new image the grid cell will be set to when you click it.

    I've set up pretty much everything except that I can't figure out how to set the background image of the cell that I clicked.

    Image:
    http://www.dreamvalleygame.com/uploa...pGenerator.png

    --
    TL;DR:
    How do I change the background image of the individual cell I click in a DataGridView?

    --

    The grid was created programmatically.

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Set SelectedEntity
            SelectedEntity = Black
    
            'Just number
            Dim n As Integer = 0
    
            'Add 80 columns
            For n = 1 To 80
                Dim newCol = New DataGridViewImageColumn
                'Set default image to a wall
                newCol.Image = Black
                'Stretch the image to fit so it looks nice
                newCol.ImageLayout = DataGridViewImageCellLayout.Stretch
                'Set the width to look nice and pretty
                newCol.Width = 20
                'Set the number of the column for easier reading
                newCol.HeaderText = n.ToString
                'Add the column
                DataGridView1.Columns.Add(newCol)
            Next
    
            'Add 60 rows
            DataGridView1.Rows.Add(60)
    
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Click DataGridView cell, change its background image

    try this. i used 4 color images that i added to my resources + 3 radiobuttons for changing SelectedEntity:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim SelectedEntity As Bitmap
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'Set SelectedEntity
    7.         SelectedEntity = My.Resources.black
    8.  
    9.         'Just number
    10.         Dim n As Integer = 0
    11.  
    12.         'Add 80 columns
    13.         For n = 1 To 80
    14.             Dim newCol = New DataGridViewImageColumn
    15.             'Set default image to a wall
    16.             newCol.Image = SelectedEntity
    17.             'Stretch the image to fit so it looks nice
    18.             newCol.ImageLayout = DataGridViewImageCellLayout.Stretch
    19.             'Set the width to look nice and pretty
    20.             newCol.Width = 20
    21.             'Set the number of the column for easier reading
    22.             newCol.HeaderText = n.ToString
    23.             'Add the column
    24.             DataGridView1.Columns.Add(newCol)
    25.         Next
    26.  
    27.         'Add 60 rows
    28.         DataGridView1.Rows.Add(60)
    29.  
    30.  
    31.     End Sub
    32.  
    33.     Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    34.         DataGridView1(e.ColumnIndex, e.RowIndex).Value = SelectedEntity
    35.     End Sub
    36.  
    37.     Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    38.         SelectedEntity = My.Resources.red
    39.     End Sub
    40.  
    41.     Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    42.         SelectedEntity = My.Resources.green
    43.     End Sub
    44.  
    45.     Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
    46.         SelectedEntity = My.Resources.blue
    47.     End Sub
    48.  
    49. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    47

    Re: Click DataGridView cell, change its background image

    Code:
    DataGridView1(e.ColumnIndex, e.RowIndex).Value = SelectedEntity
    That's all I needed and it worked! Thank you

    So I'm assuming that I can use e as the selected row and column for whenever I want to calculate the value and save to a text document?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Click DataGridView cell, change its background image

    it depends on the particular eventArgs of the handler sub you're using.
    for example:

    Code:
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DataGridView1(e.ColumnIndex, e.RowIndex).Value = SelectedEntity
    End Sub
    the DataGridView1_CellClick event has DataGridViewCellEventArgs which do contain ColumnIndex + RowIndex

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width