none
LINQ to SQL Drag and Drop DataBinding

    Question

  • Can I use Drag and Drop DataBinding in my WinForms projects to display data from two related tables in a single DataGridView control and have the DataBindingSource and DataBindingNavigator handle the details of CRUD operations for me?  Using the simple example below...

    STUDENT
    =======
    student_id
    course_id
    student_name
    grade
    -------
    COURSE
    ======
    course_id
    course_name

    I would like to display DataGridViewTextBox columns for each column in the STUDENT table except for course_id which I would like to have as a DataGridViewComboBox column so that the list of course names is available as well.

    I currently do this kind of thing programatically, but would like to know if I can simplify the process through Drag and Drop DataBinding.  I've been playing around with Drag and Drop using ADO.Net datasets, Linq to SQL classes, and a bit with Entity Framework but have not had any success.

    What's the best approach to accomplishing this (assuming it can be done easily) so I can start focussing on learning how to do that.

    Thursday, April 05, 2012 5:48 PM

Answers

  • Hi jabberpunch
    Yes, you can do all of these in the designer except the query the data by Linq to SQL.
    Here is the steps that using NorthWind’s  Order and Order_Detail  table as sample.
    First, you need to add a Linq to SQL class to your project.
    Second, create new entity classes that are mapped to related tables in the database.
    Third, create an object data source that references the entity classes.
    You can find the detail step in this link: http://msdn.microsoft.com/en-us/library/bb384428.aspx
    Fourth, open Data->show Data Sources (shift+Alt+D), drag the Order table to the Form designer. A DataBindingNavigator and DataGridView created automatically.
    Fifth, select the DataGridView , click open Edit Columns and change the key  (OrderID) column’s ColumnType to DataGridViewComboBoxColumn.
    Sixth, click DataSource property and select the related table (Order_Detail) table, then set the DataPropertyName, DisplayMember and ValueMember.

    Fifth, construct a simple LINQ query and display the results on the form.
    For example,

        public partial class Form1 : Form
        {
            private DataClasses1DataContext northwindDataContext1
    = new DataClasses1DataContext();
            public Form1()
            {           
                InitializeComponent();
                orderBindingSource.DataSource = northwindDataContext1.Orders;
                orderDetailBindingSource.DataSource = northwindDataContext1.Order_Details;
            }
        }

    Here is the result.

    If you still have any doubt and concern about this issue, please let us know. 
    Best Regards


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 12, 2012 7:13 AM
    Moderator
  • Hi jabberpunch,
    This issue occurs because the default value of combobox cell in the new created record is not a member of dropdown list.
    You can either handle the add new item button click by yourself or pass an available value to it.
    For example,

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
    {
        Order order = orderBindingSource.Current as Order;
        order.OrderID = ((Order)orderBindingSource.CurrencyManager.List[0]).OrderID;
    }
    Another way is creating Data Error event handler and leave it alone.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by jabberpunch Friday, April 20, 2012 2:16 PM
    Friday, April 20, 2012 9:18 AM
    Moderator

All replies

  • Hi jabberpunch,

    Welcome to MSDN Forum.

    I will move this case to Wndows Form Data Controls and Binding Forum, there are more related specialists there, I think you can get help more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Sunday, April 08, 2012 9:00 AM
  • Hi jabberpunch
    Yes, you can do all of these in the designer except the query the data by Linq to SQL.
    Here is the steps that using NorthWind’s  Order and Order_Detail  table as sample.
    First, you need to add a Linq to SQL class to your project.
    Second, create new entity classes that are mapped to related tables in the database.
    Third, create an object data source that references the entity classes.
    You can find the detail step in this link: http://msdn.microsoft.com/en-us/library/bb384428.aspx
    Fourth, open Data->show Data Sources (shift+Alt+D), drag the Order table to the Form designer. A DataBindingNavigator and DataGridView created automatically.
    Fifth, select the DataGridView , click open Edit Columns and change the key  (OrderID) column’s ColumnType to DataGridViewComboBoxColumn.
    Sixth, click DataSource property and select the related table (Order_Detail) table, then set the DataPropertyName, DisplayMember and ValueMember.

    Fifth, construct a simple LINQ query and display the results on the form.
    For example,

        public partial class Form1 : Form
        {
            private DataClasses1DataContext northwindDataContext1
    = new DataClasses1DataContext();
            public Form1()
            {           
                InitializeComponent();
                orderBindingSource.DataSource = northwindDataContext1.Orders;
                orderDetailBindingSource.DataSource = northwindDataContext1.Order_Details;
            }
        }

    Here is the result.

    If you still have any doubt and concern about this issue, please let us know. 
    Best Regards


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 12, 2012 7:13 AM
    Moderator
  • Hi Bob,

    I truly appreciate such a detailed answer, but that is exactly the way I've been trying to implement this.  When I follow the steps you outlined above I receive this error message:

    The error occurs when I try to Add a new record with the ToolStripButton or simply click anywhere in a new row in the DataGridView. I must be doing something wrong, just not sure what that is.


    • Edited by jabberpunch Thursday, April 12, 2012 3:10 PM
    Thursday, April 12, 2012 3:01 PM
  • Hi jabberpunch,
    I’m doing research on this issue. If I get anything, I will let you know.
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 18, 2012 9:18 AM
    Moderator
  • Hi jabberpunch,
    This issue occurs because the default value of combobox cell in the new created record is not a member of dropdown list.
    You can either handle the add new item button click by yourself or pass an available value to it.
    For example,

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
    {
        Order order = orderBindingSource.Current as Order;
        order.OrderID = ((Order)orderBindingSource.CurrencyManager.List[0]).OrderID;
    }
    Another way is creating Data Error event handler and leave it alone.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by jabberpunch Friday, April 20, 2012 2:16 PM
    Friday, April 20, 2012 9:18 AM
    Moderator
  • This issue occurs because the default value of combobox cell in the new created record is not a member of dropdown list.

    You nailed it, that's exactly what was causing my problem.  Thanks for all your help Bob!
    Friday, April 20, 2012 2:16 PM
  • You’re welcome. I’m glad to hear the issue has been solved.
    Have a nice day.

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 24, 2012 4:59 AM
    Moderator