AussieOldTimer

New member
Joined
Dec 18, 2018
Messages
1
Programming Experience
10+
Hello,
I am writing a small music player for my own personal use. The program is a Desktop application using WinForms, standard controls and the BASS DLL for the engine.
I have basically finished it but would really like to have an Album Cover view. Currently I just list the tracks in a listview, which is fine, but I'd like the option to see the covers like iTunes etc.
I believe people use the DataGridView control to do this (might be wrong), I have never used this control and when I do a web search, most (if not all) of the examples use it with a database - which is of course what it's meant to be used with. I just want to list the contents of a folder that I point it to.
Has anybody got any advice or examples of what I can do to achieve my desired result?

Cheers.

Screenshot 202dfgvbxdfbxdfgbxfgbnxfgb5-02-12 113955.png
 
When using a DataGridView with a database, the control is almost always bound to a data source, e.g. a DataTable. The data source is populated with the appropriate data and then assigned to the DataSource property of the grid. Despite not working with a database, you can do the same. You can create your own DataTable or some other appropriate list, populate it with your data and then bind it to the grid. The grid will even create the columns for you, based on the type of data in your data source, or you can create the columns yourself so they are exactly as you want. Based on your screenshot, you might want to store the cover art in the data source but not display it in the grid. When the user selects a row, you can then get the corresponding image from the data source and display it, e.g. in a PictureBox. Here's an example class that could be used to represent an item:
VB.NET:
Public Class Thing

    Public Property Name As String
    Public Property Description As String
    Public Property Image As Image
    
End Class
You could add the appropriate columns to the grid in the designer, or you could let the grid create them automatically. If you want anything other than the default columns of the default types, you should take the former route. In that case, you would need to set the DataPropertyName of each column to the name of the data source property you want to bind to. You'd want text box columns to bind to each of the Name and Description properties. When binding in code, you'd set the AutoGenerateColumns property to False to avoid creating any additional columns. You should also add a BindingSource in the designer and bind through that, e.g.
VB.NET:
Dim things As New List(Of Thing)

'Populate list here'

myBindingSource.DataSource = things
myDataGridView.AutoGenerateColumns = False
myDataGridView.DataSource = myBindingSource
You can then handle the CurrentChanged event of the BindingSource and get the record selected by the user from its Current property, displaying the data it contains as required, e.g.
VB.NET:
Private Sub MyBindingSource_CurrentChanged(...) Handles myBindingSource.CurrentChanged

    Dim currentThing = DirectCast(myBindingSource.Current, Thing)
    
    myPictureBox.Image = currentThing.Image
    
End Sub
This is not the only way to skin this cat, but other options will be similar, even if some details are different. You can add rows directly to the grid if you prefer, but I can't see how that would be of any advantage.
 
Back
Top