none
Add an image to a datagridview

    Question

  •  

    I am trying to add an image to a gridview control.

     

    My code.

     

    Dim bMap As New Bitmap("C:\Development\Red.bmp")

    Dim dgvICol As New DataGridViewImageColumn()

    dgvICol.Image = bMap

    dgv_Vehicles.Columns.Insert(1, dgvICol)

     

    I get Parameter is not valid. on line Dim bMap As New Bitmap("C:\Development\Red.bmp")

     

     

    any ideas thanks.

    Thursday, January 10, 2008 3:26 PM

Answers


  • I'm copying some code that i used in my mediaplayer app.

    I have a hidden column called dgColumnWorking. When it loads a playlist it updates these values and then i do a check to see if that value is "Y" then its a certain picture and if not its a different picture.

                        For Each Row As DataGridViewRow In Data.Rows

                            If Data.Item("dgColumnWorking", Row.Index).Value = "Y" Then

                                Data.Item("dgColumnPic", Row.Index).Value = AvailablePic

                            End If

                            If Data.Item("dgColumnWorking", Row.Index).Value = "N" Then

                                Data.Item("dgColumnPic", Row.Index).Value = UnAvailablePic

                            End If

                        Next


    "dgColumnPic" is the column where all the pictures will be displayed....
    Row.index is the current row the for...next loop is busy with



    I'm not good with explaining myself. I hope this helps.
    Thursday, January 10, 2008 4:51 PM

All replies

  • I would do it this way:


        Dim Pic As Image = _

        System.Drawing.Image.FromFile("Path of Picture")


    then you just specify the cell you want to change( Column, Row)


    Data.Item("dgColumnPic", Count).Value = Pic


    I dimmed Data as a Datagridview
    Count is the row you want to change

    The code i supplied is when the column was added before runtime.

    Hope this helps

    Thursday, January 10, 2008 3:40 PM
  • Thanks for that Lewis.

     

    Can you help me this please, i want to be able to loop through each row in the datagridview & set the image depending on a specific outcome.

     

    I've tried.

     

    Dim Pic As Image = System.Drawing.Image.FromFile("C:\Development\Image\Red.bmp")

    Dim dg As DataGridViewRow

    Dim dc As DataGridViewImageCell

     

    Dim Pic As Image = System.Drawing.Image.FromFile("C:\Development\Image\Red.bmp")

    Dim dg As DataGridViewRow

    Dim dc As DataGridViewImageCell

     

    Me.dgv_Vehicles.Item("In_Service", 1).Value = Pic

     

    For Each dg In dgv_Vehicles.Rows

    For Each dc In dg.Cells

    dc.Value = Pic

    Next

    Next

     

    Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewImageCell'.

     

    at Line

    For Each dc In dg.Cells

    Thursday, January 10, 2008 4:35 PM

  • I'm copying some code that i used in my mediaplayer app.

    I have a hidden column called dgColumnWorking. When it loads a playlist it updates these values and then i do a check to see if that value is "Y" then its a certain picture and if not its a different picture.

                        For Each Row As DataGridViewRow In Data.Rows

                            If Data.Item("dgColumnWorking", Row.Index).Value = "Y" Then

                                Data.Item("dgColumnPic", Row.Index).Value = AvailablePic

                            End If

                            If Data.Item("dgColumnWorking", Row.Index).Value = "N" Then

                                Data.Item("dgColumnPic", Row.Index).Value = UnAvailablePic

                            End If

                        Next


    "dgColumnPic" is the column where all the pictures will be displayed....
    Row.index is the current row the for...next loop is busy with



    I'm not good with explaining myself. I hope this helps.
    Thursday, January 10, 2008 4:51 PM
  • How many image columns do you have?

    I see now that you went through each row and then each cell of that row.... This would not work if all the collumns are not images.

    Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewImageCell'.

     

    at Line

    For Each dc In dg.Cells


    Thats why you are getting this error. Your trying to set a textbox column to an image...

    Hope it helps
    Thursday, January 10, 2008 4:57 PM
  • Thanks for that its similar to what i want to do.

     

    If my flag is set to "y" then i want image 1 to be displayed, otherwise if the flag is set to "n" then i want image 2 to be displayed.

     

     

     

    Friday, January 11, 2008 10:17 AM