top of page
Search

DAT BLOG POST 13

  • daniel-ewers2
  • Jun 21, 2021
  • 3 min read

Sessions 20-21 – GUI COMPONENTS


What Have I Learned?

This week I learned about GUI components, more specifically, datagridviews. DataGridViews are a very useful control for displaying data from a data source. There are a couple of good ways that I have learned through self-taught learning for connecting the datagridview to a data source. The two types of data sources that I have worked with are lists and a database. By using a data source property on a datagridview, we are able to bind that data to the control. Once bound, we can easily view and manipulate the data.


Any changes to the data source can easy updated on the display just by setting the data source of the control to a new instance of the same data source, as shown below.



ree

Every time the data is changed in the data source, we can rebind the data and update the display by calling the method above. The downside to using a data source is that you can’t perform the same methods for adding and sorting as you would with a datagridview that is not data bound. To my knowledge, when you bind a data source to a datagridview, by default it also automatically generates all of the columns, thus requiring you to hard code remove any that you do not want to display.


There are many cool features that datagridviews have that you can enable if you want. Such as making setting a column type to be either a button column, combo box column, check box column, image column, link column, or text box column. This opens a wide range of possible uses for our datagridviews.

Below is an example of a datagridview that I implemented into one of my other assessments:

ree

This datagridviews data source is bound to a list of objects within a specific class. Each object has the four properties displayed across the column headers. There are two ways that I tried to achieve adding this data to the datagridview. One way was using a foreach loop that ran through the list, while the other used a binding source for the datagridviews data source. The issue I found with the foreach loop was that it was not directly referencing the list (almost like a clone), so any data manipulation would not change the data that was in source list. Whereas, using a binding source for the data source directly referenced each object in the list.


Another issue I ran into when using a binding source was that you cannot use the column headers to sort the rows by that columns data. When using a binding source, this throws an error saying that you can’t sort the datagridview as it is data bound. Therefor, you need to implement the sorting method when you set the data source of the datagridview. The method I used to fix this was to have a button that has a method that checks a bool value. If the value is true, then it sorts the source list by ascending, else descending.



Why Have I Learned This?

I believe that I have learned this as datagridviews are a very easy and useful tool for displaying data from a data source. There are other methods that we could use such as list boxes or multiline text boxes, but datagridviews seem to have a larger range of features while also having a lot more room for design manipulation. I have found that my experience with datagridviews has been far better for my learning than trying to use other controls like list boxes. There have been a few hurdles with learning how to use them as you have to make a few decisions along the way of how you wish to implement them.



How Have I Learned This?

I have learned this through a lot of trial and error as well as watching YouTube tutorials on different ways of using datagridviews. I have spent a lot of time practising different techniques of how I can implement them as well as playing around with different configurations.

 
 
 

Comments


Post: Blog2_Post
  • Facebook
  • Twitter
  • LinkedIn

©2021 by Dwewers2021 ~ Blog. Proudly created with Wix.com

bottom of page