1

I am working on a gridview that can perform add, edit and delete operations on master tables in a database. On Edit operation, It Should populate all foreign key values((if any) in a dropdown control.

I came across dynamicdataManager control to be used for this scenario and found it can be easily used in Asp.net web forms and then there is no need to create a dedicated Dynamic data website.

I am able to bind the data with LinqDataSource but I am unable to load foreign key values in one of the columns. I have set up two table in the database for creating the sample.

Structure of tables are :

CREATE TABLE [dbo].[LK_CardType](
    [CardTypeID] [int] IDENTITY(1,1) NOT NULL,
    [CardTypeName] [nvarchar](50) NOT NULL,
    [AdditionalNotes] [nvarchar](50) NOT NULL,
    [Ref_ID] [int] NULL, --this is referring to another table called RefTes(ReferenceTest)
 CONSTRAINT [PK_LK_CardType] PRIMARY KEY CLUSTERED (    [CardTypeID] ASC ) ) ON [PRIMARY]

GO
ALTER TABLE [dbo].[LK_CardType]  WITH CHECK ADD  CONSTRAINT [fk_cardtype_RefTest] FOREIGN KEY([Ref_ID])
REFERENCES [dbo].[RefTest] ([Ref_Id])
GO

Parent Table

CREATE TABLE [dbo].[RefTest](
    [Ref_Id] [int] IDENTITY(1,1) NOT NULL,
    [Ref_Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_RefTest] PRIMARY KEY CLUSTERED 
(
    [Ref_Id] ASC
)
) ON [PRIMARY]

I have create a LINQDataSource for these two tables and the class relationship is as displayed in the diagram below :

enter image description here

Now, I have dynamic fields in my gridview to show list of LK_CardTypes and one of which refers to the the name of the referenced class "RefTest". When I run my page, I get to see class name instead of a value from the referenced table "RefTest".

I know I am missing something since I haven't mentioned anywhere in the code that Ref_Name is mapped to Ref_Id.

My Html Markup for the gridview is :

<table>
<tr>
        <td colspan="3">
   <asp:DynamicDataManager ID="DynamicDataManager1"    AutoLoadForeignKeys="true"  runat="server">


     <%-- <DataControls><asp:DataControlReference ControlID="GridView1"  /></DataControls> --%>


      </asp:DynamicDataManager>
        </td>
    </tr>
</table>

    <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" 
        DataKeyNames="CardTypeID" DataSourceID="LinqDataSource" >
        <Columns>
               <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                   ShowSelectButton="True" />
               <asp:DynamicField DataField="CardTypeID" />
               <asp:DynamicField DataField="CardTypeName" />
               <asp:DynamicField DataField="AdditionalNotes" />
               <asp:DynamicField DataField="RefTestValue" />

</Columns>
    </asp:GridView>
        <asp:LinqDataSource  ID="LinqDataSource" runat="server" 
            ContextTypeName="JqueryGrid_MasterData.MasterTableDataContext" 
            EntityTypeName="" TableName="LK_CardTypes"  EnableDelete="True" 
            EnableInsert="True" EnableUpdate="True">
        </asp:LinqDataSource>

On the Page_Init Event, I have enabled the dynamic data on the gridview and have registered the control with the DynamicDataManager control

  GridView1.EnableDynamicData(typeof(LK_CardType));
          DynamicDataManager1.RegisterControl(GridView1);

Do I need some setting at the field level of Gridview control or need some setting in LinqDataSource ?

Kunal
  • 1,913
  • 6
  • 29
  • 45
  • Did you solve this? I see that you posted the same question on the aspnet forums. I'm having similar problems here and I think this is impossible to achieve without registering the whole DataContext in the global.asax file. The EnableDynamicData method uses a simpler MetaTable generator that does not consider related models at all. – julealgon Dec 10 '13 at 15:44

1 Answers1

0

You might be able to make this work:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="CardTypeID" DataSourceID="LinqDataSource">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
             ShowSelectButton="True" />
        <asp:DynamicField DataField="CardTypeID" />
        <asp:DynamicField DataField="CardTypeName" />
        <asp:DynamicField DataField="AdditionalNotes" />
        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("RefTest.Ref_Name") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Of course you won't be able to edit the RefTest.Ref_Name value

kevev22
  • 3,737
  • 21
  • 32
  • But idea is not just to show the name, idea is to have similar behaviour which we get on dynamic data website so I need all Add, Edit and delete operations. I have seen some some experts suggesting to copy the entire dynamic folder from a dynamic data web application tempate to an existing web forms application. – Kunal Nov 10 '11 at 22:50