Working with DataGridView (Win)

Posts   
 
    
ftuttle
User
Posts: 40
Joined: 10-Dec-2006
# Posted on: 16-Mar-2007 22:02:28   

Is there a sample that demonstrates the best practices for working with abound data grid view control. I am specifically interested in Row Deletes and Saving Changes (Eidts and Add) back to the DB. I am not sure how much event trapping and keeping track of what the user is doing that I need to account for as opposed to what the generated code may be doing.

A sample would really eliminate a lot of questions and reduce the learning curve when trying to discover the best way of doing something.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 17-Mar-2007 02:08:12   

http://www.llblgen.com/pages/examples.aspx has some complex databinding examples that may be of help along with other examples.

ftuttle
User
Posts: 40
Joined: 10-Dec-2006
# Posted on: 17-Mar-2007 02:57:01   

I have looked at those. The Complex Data binding for VS 2005 (Win) is the one I was very interested in but it does not demonstrate DataGridView Add/Update saving and Row deleting. I would really like to see some examples of working with the events from the grid and identifying the relation between the current item in the bound DataGrid and the EntityCollection. I am doing most things in code to learn it well first before just relying on data binding from a data source.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Mar-2007 02:55:05   

Hi. The Complex databinding example (VS.NET 2005)_ "shows how to setup a winforms form using complex databinding to get a grid which is easy to edit due to the combo-boxes filled with related entity data." (Ref. Readme.txt)_

Some little instructions for that example:

  • For adding, simply add a new row (* row) to the grid. No code is necessary.
  • For updating, simply edit the row you want. No code is necessary.
  • For deleting catch the UserDeletingRow event so you can add this entity to a collection to delete later. (you can delete right away if you want).
  • For save the whole changes, simply add a button to persist (SaveMulti) the customer collection recursively. (Ref. Readme.txt).

Example of saving changes (only relevant code):

public partial class MainForm : Form
{
    // move to here so other methos can use it
    CustomerCollection customers;
    OrderCollection ordersToDelete;

    public MainForm()
    {
                ...     

                // load all customers and bind them to the combobox
        customers = new CustomerCollection();

                ...             

                // initialize orders to delete collection
                ordersToDelete = new OrderCollection();
    }
        
         ...

private void btnSaveChanges_Click(object sender, EventArgs e)
{
    /// save all objects.
    customers.SaveMulti(true);
    
    /// delete objects marked to delete
    ordersToDelete.DeleteMulti();
}

private void _ordersGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    OrderEntity orderSelected = ((OrderEntity)e.Row.DataBoundItem);
    ordersToDelete.Add(orderSelected);
}

VSNet Help have a lot of information about DataGridView event handling. Good luck wink

David Elizondo | LLBLGen Support Team