none
Binding DataGridView to Table<TEntity> RRS feed

  • Question

  • I'm trying to bind a DataGridView to a LINQ-to-SQL Table<TEntity>. If possible, I want the user to be able to add and remove records normally in the DGV. I am currently binding the DGV to a BindingSource which is bound to a Table. How can I get changes to propagate into my DataContext properly? I really want the new records to be usable without manually calling SubmitChanges(), and I definitely want to avoid doing it manually with Insert/DeleteOnSubmit(). Is there some way for a BindingSource to do the insert/delete stuff for me? I'm really confused as to how to work with the Table's and DataContext's less-than-convenient data management.
    Tuesday, May 31, 2011 4:17 PM

Answers

  • Hi kendfrey;

    To your question, "I want the user to be able to add and remove records normally in the DGV", to insure that this happens make sure that the DGV Smart tag has the following enabled Enable Adding, Enable Editing, and Enable Deleting are checked.

    To your question, "I am currently binding the DGV to a BindingSource which is bound to a Table", just for cclaritythe bindingsource is bound to the Linq to SQL query result set.

    Yo your question, "How can I get changes to propagate into my DataContext properly?", that is handled by the binding source control, it will take care of that.

    To your statement, "I really want the new records to be usable without manually calling SubmitChanges(), and I definitely want to avoid doing it manually with Insert/DeleteOnSubmit()", to the part of the statement, "I definitely want to avoid doing it manually with Insert/DeleteOnSubmit()", this is answered in the above question. As to this, "I really want the new records to be usable without manually calling SubmitChanges()", although the binding source takes care of keeping the DataContext in sync you are resresponsible call SubmitChanges() to keep the data store in sync.

    To your question, "Is there some way for a BindingSource to do the insert/delete stuff for me?", yes, answered above.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by kendfrey Monday, June 6, 2011 9:57 AM
    Tuesday, May 31, 2011 5:33 PM
  • I think my problem was that changes in one part of my DataContext did not propagate to other parts. I did find that the DGV was updating part of the DataContext, so I think if i call SubmitChanges, it should show up in the rest of the data.

    One thing I was wondering: What is the best way to add a record to a table? The table I need this for is a parent table, so I believe I should use InsertOnSubmit and SubmitChanges, correct?

    • Marked as answer by kendfrey Monday, June 6, 2011 9:57 AM
    Monday, June 6, 2011 9:57 AM

All replies

  • Hi kendfrey;

    To your question, "I want the user to be able to add and remove records normally in the DGV", to insure that this happens make sure that the DGV Smart tag has the following enabled Enable Adding, Enable Editing, and Enable Deleting are checked.

    To your question, "I am currently binding the DGV to a BindingSource which is bound to a Table", just for cclaritythe bindingsource is bound to the Linq to SQL query result set.

    Yo your question, "How can I get changes to propagate into my DataContext properly?", that is handled by the binding source control, it will take care of that.

    To your statement, "I really want the new records to be usable without manually calling SubmitChanges(), and I definitely want to avoid doing it manually with Insert/DeleteOnSubmit()", to the part of the statement, "I definitely want to avoid doing it manually with Insert/DeleteOnSubmit()", this is answered in the above question. As to this, "I really want the new records to be usable without manually calling SubmitChanges()", although the binding source takes care of keeping the DataContext in sync you are resresponsible call SubmitChanges() to keep the data store in sync.

    To your question, "Is there some way for a BindingSource to do the insert/delete stuff for me?", yes, answered above.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by kendfrey Monday, June 6, 2011 9:57 AM
    Tuesday, May 31, 2011 5:33 PM
  • After adding a row in the DGV (it is a child by the way), there is no new record in the DataContext. Your answer "it works as-is" does not work.

    I forgot to mention that I have a BindingSource which is bound to the parent table, and a BindingSource which is bound to the current record of the first one. My DGV uses the second. Could this be an issue?

    Tuesday, May 31, 2011 5:49 PM
  • Show Me The Code
    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, May 31, 2011 8:04 PM
  • This is the code relevant to the main form and DGV:

    public partial class MainForm : Form
    {
    	BindingSource CustomersBS;
    	BindingSource OrdersBS;
    	
    	public MainForm()
    	{
    		InitializeComponent();
    		CustomersBS = new BindingSource(Program.MainDataContext, "Customers");
    		OrdersBS = new BindingSource(CustomersBS, "Orders");
    		
    		tbCustomerName.DataBindings.Add(new Binding("Text", CustomersBS, "Name"));
    		
    		dgvOrders.AutoGenerateColumns = false;
    		dgvOrders.Columns["Number"].DataPropertyName = "Number";
    		dgvOrders.Columns["Total"].DataPropertyName = "Total";
    		dgvOrders.DataSource = OrdersBS;
    	}
    }
    

    • Proposed as answer by TylerGG Monday, June 6, 2011 4:59 AM
    Thursday, June 2, 2011 2:06 PM
  • Hi kendfrey,

    I think if you just use datasource without databinding, you can use BeginEdit event and EndEdit event to updatedate or create new record.

    Or you can use DataBinding to implement your issue, here is a sample on the codeproject:

    Implementing complex data binding in custom controls:
    http://www.codeproject.com/KB/database/DataBindCustomControls.aspx

    And there is an example on MSDN Library and it shows what you want:

    How to: Bind Data to the Windows Forms DataGridView Control:
    http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

     

    Best Regards


    Knowledge will change the destiny.
    • Proposed as answer by TylerGG Monday, June 6, 2011 5:06 AM
    Monday, June 6, 2011 5:06 AM
  • I think my problem was that changes in one part of my DataContext did not propagate to other parts. I did find that the DGV was updating part of the DataContext, so I think if i call SubmitChanges, it should show up in the rest of the data.

    One thing I was wondering: What is the best way to add a record to a table? The table I need this for is a parent table, so I believe I should use InsertOnSubmit and SubmitChanges, correct?

    • Marked as answer by kendfrey Monday, June 6, 2011 9:57 AM
    Monday, June 6, 2011 9:57 AM