LINQ to objects with linqdatasource and DetailsView

Unanswered LINQ to objects with linqdatasource and DetailsView

  • Wednesday, December 19, 2007 4:38 AM
     
     
    VS 2008 professional C#
    Hello ! I have a class with properties and I need to give the user a way to edit it. The best for me is to use a DetailsView. So far I managed to set the DetailsView.DataSource only in code. But I would like to be able to define some templates for edit. I tried to set a LinqDataSource and set its ContrxtTypeName and TableName and and set the DataSourceID of a DetailsView to it. I was hoping to see my class properties in the wizard opene when pressing the "Edit Fields" of the DetailsView so I can define what I need to.  Well - no luck !! Is there a way to do this ? Is LINQ to objects accessible at design time?
    I read many post on LINQ to Objects, and most of them show examples using the console.
    Thanks in advance   Gil Yoktan

All Replies

  • Saturday, December 29, 2007 3:32 PM
     
     
    Hello Gil,

    I tried what you describe, and it worked for me. Here is what I did.
    1. Create a new ASP.NET Web Application with VS
    2. Create two classes: MyDataContext and MyClass. See below.
    3. Drop a LinqDataSource and a DetailsView on the default page
    4. Set the LinqDataSource as the DataSource for the DetailsView
    5. Set ContextTypeName = "WebApplication1.MyDataContext" and TableName = "MyTable" on the LinqDataSource
    6. Click "Edit Fields..." on the DetailsView
    I see the properties in the list of selected fields and under BoundField. Note: you may have to click "Refresh Schema" on the dialog.
    Does it work for you this way?

    Here are my classes:

    public class MyClass
    {
      public int IntProp { get; set; }
      public String StringProp { get; set; }
    }

    public class MyDataContext
    {
      public IEnumerable<MyClass> MyTable { get; set; }
    }

    Fabrice
  • Sunday, December 30, 2007 4:02 PM
     
     

    Additional information: to provide the data to the LinqDataSource, you can use the ContextCreating event of the LinqDataSource.

    1. Add OnContextCreating="LinqDataSource1_ContextCreating" to the LinqDataSource's tag
    2. Add the following method to Default.aspx.cs:

    Code Block

    protected void LinqDataSource1_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
    {
      var context = new MyDataContext();
      context.MyTable = new[] {
        new MyClass { IntProp = 1, StringProp = "Un" },
        new MyClass { IntProp = 2, StringProp = "Deux" }
      };
      e.ObjectInstance = context;
    }

     

     

  • Wednesday, March 25, 2009 9:39 AM
     
     
    Hello Fabrice. Thanks for responding so quickly
    Maybe I forgot to say that I work with winforms
    I downloaded an example from the net working with the Northwind database, and it works OK. I tried to change the database to mine with tables with foreignkeys as in the Northwind DB. In the example the work is
    1 - create a class with the dbml
    2 - compile it
    3 - create another project in the same solution with a form
    4 - create a datasource -> object from the dbml
    5 - drag the table to the form

    In my implementation the master table has the datagridview option and the details table - does NOT
    What I ask is what is the Microsoft algo which fills the dropdown list - with or without the datagridview option?

    Any help will do
    Thanks in advance  Gil Yoktan