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 :
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 ?