help with BindingSources

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BobLewiston
    New Member
    • Feb 2009
    • 93

    help with BindingSources

    I need some help with BindingSources.

    This is what I'm working on:

    From the C# tutorial at Programmer's Heaven (http://www.programmersheaven.com/2/Les_CSharp_13_p9):

    private void btnLoadData_Cli ck(object sender, System.EventArg s e)
    {
    string connectionStrin g = "server=P-III; database=progra mmersheaven; uid=sa; pwd=;";
    SqlConnection conn = new SqlConnection (connectionStri ng);
    string cmdString = "SELECT * FROM article";
    SqlDataAdapter dataAdapter = new SqlDataAdapter (cmdString, conn);
    DataSet ds = new DataSet ();
    dataAdapter.Fil l (ds, "article");
    dgDetails.SetDa taBinding (ds, "article");
    }

    When I try to compile this, I get the following error: "The name 'dgDetails' does not exist in the current context".

    I have just been told that dgDetails is a BindingSource. I'm a newbie, so I'm not familiar with BindingSources.

    Could you tell me what I need to include in my code to get the compiler to recognize the dgDetails BindingSource and compile my app?
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    You'll need to drop a BindingSource component onto the same form that hosts the btnLoadData button.

    Get the form up in the designer, open the Visual Studio toolbox and you should see a group called components. Grab a bindingsource and drop it onto the form. Rename it to 'dgDetails'. That should do it.

    For more info on bindingsources see here

    By the way, I couldn't get the link you provided to work.

    Comment

    • BobLewiston
      New Member
      • Feb 2009
      • 93

      #3
      nukefusion:

      I'm a newbie who is kind of stumbling around in the dark, so I misstated the situation. Although I had been told that by someone that BindingSources was what I should research, I have now been advised by someone else that dgDetails refers to my DataGridView object, so I should just change dgDetails to the name of that object. I'm sorry to have put anyone to the trouble of answering the wrong question.

      At any rate, now I have a new problem. I get the following compile error:

      'System.Windows .Forms.DataGrid View' does not contain a definition for 'SetDataBinding ' and no extension method 'SetDataBinding ' accepting a first argument of type 'System.Windows .Forms.DataGrid View' could be found (are you missing a using directive or an assembly reference?)

      Maybe I just need to include a namespace containing the right definitions (I am already using System.Windows. Forms). Any ideas?

      By the way, I couldn't get the link you provided to work.
      Sorry, it's the parentheses and colon surrounding the URL that's the problem. You can just copy and paste the URL (without the parentheses and colon) into your browser and that will work fine.

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        That's okay, maybe I should have noticed that - the prefix 'dg' obviously stands for datagrid.

        Your problem now is due to the fact that the DataGridView object doesn't have a 'SetDataBinding ' method, however the 'DataGrid' control does (DataGrid and DataGridView are two seperate controls). The tutorial you're looking at is using a DataGrid control.

        To get your code to work you'll need to drop a DataGrid control onto your form instead of the DataGridView.

        If you can't find the DataGrid in the toolbox you'll have to right-click an area within the toolbox and select 'Choose Items', then locate the DataGrid control from there.

        Comment

        • BobLewiston
          New Member
          • Feb 2009
          • 93

          #5
          nukefusion:

          VC#'s Help directed me to http://msdn.microsoft.com/en-us/libr....datagrid.aspx, which says:

          "The DataGridView control replaces and adds functionality to the DataGrid control..."

          VC#'s toolbox has a DataGridView control but no DataGrid control. So I did 'Choose Items', and installed a DataGrid control.

          Now, when the statement
          Code:
          dataGrid1.SetDataBinding (dataSet, "Person.Contact");
          is executed, an exception is thrown with the following message:
          Child list for field Person cannot be created.
          Any idea what that means?

          For what it's worth, I'm working in a 32-bit environment with the following software:

          SQL Server 2008 Express with Advanced Services
          database: SQL2008 AdventureWorks (schema.table: Person.Contact)
          SQL Server 2008 Management Studio Express
          Visual C# 2008 Express

          Comment

          • nukefusion
            Recognized Expert New Member
            • Mar 2008
            • 221

            #6
            I notice that line of code isn't in your previous sample or in the online tutorial. Is this a different portion of code? It may help to see how you are loading the data.

            I don't have access to a copy of the AdventureWorks 2008 sample database at the moment, so I haven't been able to check, but from the exception detail it sounds like for whatever reason, a list of Contacts for Person cannot be found. I've got the AdventureWorks 2000 database and there's no "Person" table in there....

            Are you sure there is a "Person" object that is linked to a "Contact"? Has the dataset been filled correctly?

            An easier way to get the datagrid to bind to data might be to set the datasource and datamember properties instead.

            Comment

            Working...