Search code examples
vb.netdatagridviewbindingsource

DataGridView DataSource // integer to icon


I've got a list-based application that runs very slow, because each row in my DataGridView is build manually as DataGridViewRow. To fix performance issues i decided to use a DataSource (DataTable) instead.

My problem:

One column I am recieving from my database is filled with image IDs. The application knows which image belongs to which id. When creating gridrows manually there was no problem in translating int to a bitmap and use the bitmap as value for an imagecolumn. Now using the Datatable instead i cant get it working. For testing i wrote the following class:

Public Class test
Dim img As Bitmap

Public Sub New(image As Bitmap)
    InitializeComponent()
    Me.img = image
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt As New DataTable
    dt.Columns.Add("StrCol")
    dt.Columns.Add("ImgCol")
    Dim row As DataRow = dt.NewRow
    row.Item(0) = "Ohne Bild"
    row.Item(1) = 0
    Dim row2 As DataRow = dt.NewRow
    row2.Item(0) = "Mit Bild"
    row2.Item(1) = 1
    dt.Rows.Add(row)
    dt.Rows.Add(row2)
    DataGridView1.DataSource = dt
End Sub

Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If e.ColumnIndex = 1 Then
        If e.Value = 0 Then
            e.Value = Nothing
        Else
            e.Value = img
        End If
    End If
End Sub
End Class

hint:

  • "Ohne Bild" translates into "Without image"
  • "Mit Bild" translates into "With image"

Output after Button1_Click() Output after Button1_Click()


EDIT

Due to a missunderstanding I will try to clarify my question;

By list-based i dont actually mean the list-object. My bad there.By list-based i mean an application that displays the user Database entrys he made. The very basic of my application is simple: Allow user to add Rows, Edit and delete rows in a datagridview. This information is saved and edited in a database. Some of these informations in the rows (2 columns to be exactly) are displayed as icons while the application simply saved an id for the icon in the database. So the application knows which ID belongs to which icon.

The sample dt in the question is just to have something to work with. In the application it will be data recieved from a database to a datatable.


Solution

  • I finally solved it myself.

    1. I get a datatable from my database.
    2. Then I add a Column to it with the DataType Bitmap
    3. Then I loop through each row of the DataTable and translate the Image_ID column to the image itself and put the image into the new Column
    4. Then I remove the Image_ID column
    5. Then I use the new DataTable as DataSource

    I created that code in my main application which is a lot more complex so I can't show the code here. Sorry for that and thanks for your help.

    It is important to do every change you need to do before using the table as source, because as soon as the gridView has to change something that is displayed it takes a lot more time.