WhenI decided that the WPF Datagrid was the answer to displaying the MySQL data I hadn’t realised how convoluted a journey I was embarking upon. Using the MySQLClient Itemsource as my data source turned out to be the easy part of the puzzle.
Using the WPF Datagrid should have been far easier than trying to code the workings from scratch. And, having resolved the issues, it is a wonderfully elegant solution.
I finally found the answer under the unlikely subject title of How to get the value of a HIDDEN Datagrid cell from VBForums.
I am using Visual Studio 2019 and MySQL 8.0 for this project. So it was obvious that I would use the MySQL Connector/ODBC 8.0.17 as my interface with Visual Studio. This comes with the MySQLClient object library.
I am already using this library for all of my current data connectivity with Visual Studio. I have download Visual Studio 2019 and found it to be very intuitive and friendly.
It is a world apart from the Visual Basic 6.0 I used many years ago. I think that many developers work with C#. I did consider going down this road but, I am more comfortable with Visual Basic.
Enough of the preamble, I am here to report that configuring the Datagrid is fairly straight-forward with a MySQLClient Itemsource.
Using MySQLClient Itemsource
Visual Studio employs WPF or Windows Presentation Foundation. This is a beautiful, object-oriented programming environment that I would heartily recommend.
I opted to look at the Datagrid object for two reasons:
- Never having used a Datagrid, I was keen to become familiar with the problems and possibilities.
- I needed an ordered list that couldn’t be satisfied with a ListBox or a ComboBox.
The data was to come from a MySQL database, so I began trawling the Internet with the help of Google, to find a workable solution.
However, it quickly became apparent that the answer would not be found directly.
So, I had to resort to some lateral thinking. The data source needs to be fed a dataset. And, as far as I could see the MySQLClient that I was already using supplied this. Although, it is not referred to as such in any documentation.
Furthermore, nothing ventured is nothing gained. I set about building a Datagrid application.
You will see that I have provided only a very simple interface.
This is a deliberate approach, to test the functionality of the code before attempting to enhance the appearance.
Because functionality is what I have set out to provide. And, I want to show-off my code, not my artistic flair.
The following code sample shows that the flexibility of WPF allows me to populate the grid with only two object declarations and four lines of code.
The first line of code: MyCmd = conn.CreateCommand() creates the command object using the database connecter conn.
The next line allows me to set the SQL code into the command object which will return the data rows from my MySQL database.
I haven’t shown the code to establish the connection because that is equally brief and simple.
Then we create the Reader object which will form the dataset we need to populate our Datagrid.
Finally, we set the Itemsource property to the Reader to actually fill the grid.
How to read Datagrid cell contents
I thought, and you may also have thought, that reading the data back from the Datagrid would be quite a simple task to achieve.
But, I had to spend many hours and read through hundreds of web pages and forum threads before I found the answer,
While a Datagrid looks like a well-structured collection of grid cells, ordered in rows and columns. Each row is a collection of Datagriditems that need to be decoded to extract the visible data.
The code to facilitate this is not easily comprehended. And I still have to think very carefully to understand what it is doing.
All of the magic happens in a single line of object declaration.
Dim id As Integer = Convert.ToInt32(DirectCast(DataGridPosts.Columns(0).GetCellContent(DataGridPosts.SelectedItem), TextBlock).Text)
We use the DirectCast function to extract the data from the SelectedItem from column(0) using the GetCellContent function.
This only works because column 1 of my Datagrid holds integer values in the shape of the ID column of the database.
A beautifully simple solution to an initially complex problem, I hope you agree.
However, Having to read through so many pages of code from other developers has shown me that I can improve the appearance of my XAML code.
Streamline your XAML code to improve readability
You may have noticed when browsing WPF forums that some lines of XAML code can be rather verbose. It is easy to code XAML in single lines, when with a little more effort the readability can be improved,
The single line of XAML to define the Datagrid spills over several screens.
But, by introducing line breaks extra white space, we can make the coe much easier to read and understand.
The same code as had been shown earlier is now much easier to read and understand.
This has the added advantage of making the code look more professional. A definite boon if you intend to publish your work.
Cognitive Improvement Workout is supported by advertising the products and services that we use and love.
We use Bluehost and Siteground to host our group sites. Traffic comes from Pinterest aided by Tailwind and Tasty Pins. We rely on BlogVault for backup and the essential security is provided by Sucuri.