none
Better way to filling DataGridView RRS feed

  • Question

  • Hi,

    I want to know what is better for filling the DataGridView :
    1- Put it to a dataset and bind dataset to DataGridView then send dataset to database? 
    2- Put it directly to DataGridView and sending DataGridView to database?

    Wednesday, October 11, 2017 10:04 PM

Answers

  • It's dependent on the behavior you want e.g. if the users will be working with data add/edit/delete directly from the DataGridView and you are new to this then using TableAdapter/BindingSource/DataSet is one way. If you have good skills with data and wanted to do the above it will take a good deal more code e.g. working with perhaps DataTable events (see my MSDN code sample) which trigger when an operation happens or via GetChanges method of a DataTable (see my MSDN code sample). 

    Then there is working with SqlClient connection/command using MERGE INTO (see my MSDN code sample).

    You could use Entity Framework (See my MSDN code sample) to work out of a DataGridView also.

    Most of the options listed above will take you time to feel comfortable with, there is no magic method that suits everyone's needs without getting a bit into write a decent amount of code especially when considering business rules against the data etc.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 11, 2017 11:26 PM
    Moderator

All replies

  • It's dependent on the behavior you want e.g. if the users will be working with data add/edit/delete directly from the DataGridView and you are new to this then using TableAdapter/BindingSource/DataSet is one way. If you have good skills with data and wanted to do the above it will take a good deal more code e.g. working with perhaps DataTable events (see my MSDN code sample) which trigger when an operation happens or via GetChanges method of a DataTable (see my MSDN code sample). 

    Then there is working with SqlClient connection/command using MERGE INTO (see my MSDN code sample).

    You could use Entity Framework (See my MSDN code sample) to work out of a DataGridView also.

    Most of the options listed above will take you time to feel comfortable with, there is no magic method that suits everyone's needs without getting a bit into write a decent amount of code especially when considering business rules against the data etc.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 11, 2017 11:26 PM
    Moderator
  • Thanks,

    do you think using second method, datagrid for only simple Add/delete would be a good code?

    Thursday, October 12, 2017 9:04 AM
  • Thanks,

    do you think using second method, datagrid for only simple Add/delete would be a good code?

    Other than working with TableAdapter method, all of the others are viable dependent on your dedication to learn one of them.

    Using the GetChanges (if that is what you refer to as the second method) does make it easy to discover the changes so from there you would have a method in a class for each operation e.g. one for read, one for update, one for add and one for delete. I don't have a full example for using this method but say if you study the GetChanges example I provided along with my basic code sample on crud operations, combine them together in your own way you should be good to go. Note in the constructor if the basic code sample this could be done differently e.g. simple load into a DataTable yet for that demo I did it for a specific reason which may not suit your needs.

    If you were to look at this code sample, I did a different method to work with data in regards to reading then the basic code sample so you can see not all projects are the same yet if I were doing Entity Framework in several projects I would be using the same pattern in each. At work I use Entity Framework exclusively (and with that we only do web apps but if we were doing desktop apps I would still be using Entity Framework).


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 12, 2017 9:32 AM
    Moderator