locked
Adding dynamic data behavior to gridviewewo RRS feed

  • Question

  • User-389597101 posted

    Hi All 

    I want to add dynamic data behavior in grid view. For that I have followed these steps

    1. Create an entity framework.

    2. Bind grid view with entity framework using following code

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MyDDExampleEntities entities = new MyDDExampleEntities();
                gvTest.DataSource = (from element in entities.Classes select element);
                gvTest.DataBind();
    
            }
        }

    Then added the code on page_init event

    protected void Page_Init(object sender, EventArgs e)
        {
            gvTest.EnableDynamicData(typeof(Class));        
        }

    In aspx page I have added following code

    <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DynamicControl ID="ctrlClassId" DataField="ClassId" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DynamicControl ID="ctrlClassCode" DataField="ClassCode" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DynamicControl ID="ctrlClassDisplayName" DataField="ClassDisplayName" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>    

    But I could see the records but I could not see the dynamic behavior. Am I missing anything?

    Sunday, March 23, 2014 7:27 AM

Answers

All replies

  • User-389597101 posted

    Hi

    I have moved a little ahead now I have bound the grid view with entity framework instead of code behind (so I have removed Page load event). Now my aspx page looks like

    <asp:DynamicDataManager ID="src1" runat="server">
            <DataControls>
                <asp:DataControlReference ControlID="gvTest" />
            </DataControls>
        </asp:DynamicDataManager>
        <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false" DataSourceID="EntityDataSource1">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DynamicControl ID="ctrlClassId" DataField="ClassId" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DynamicControl ID="ctrlClassCode" DataField="ClassCode" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DynamicControl ID="ctrlClassDisplayName" DataField="ClassDisplayName" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=MyDDExampleEntities"
            DefaultContainerName="MyDDExampleEntities" EntitySetName="Classes">
        </asp:EntityDataSource>

    But now I am getting following error - "Specified method is not supported." and here is the stack trace

    [NotSupportedException: Specified method is not supported.]
    System.Web.DynamicData.ModelProviders.SimpleDataModelProvider.CreateContext() +29
    System.Web.DynamicData.MetaTable.CreateContext() +16
    System.Web.DynamicData.MetaTable.System.Web.DynamicData.IMetaTable.CreateContext() +10
    System.Web.DynamicData.<>c__DisplayClass4.<RegisterControlInternal>b__1(Object sender, EntityDataSourceContextCreatingEventArgs e) +14
    System.Web.UI.WebControls.EntityDataSourceView.OnContextCreating(EntityDataSourceContextCreatingEventArgs e) +112
    System.Web.UI.WebControls.EntityDataSourceView.ConstructContext() +62
    System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +76
    System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
    System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
    System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
    System.Web.UI.WebControls.GridView.DataBind() +4
    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
    System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75
    System.Web.UI.Control.EnsureChildControls() +102
    System.Web.UI.Control.PreRenderRecursiveInternal() +42
    System.Web.UI.Control.PreRenderRecursiveInternal() +175
    System.Web.UI.Control.PreRenderRecursiveInternal() +175
    System.Web.UI.Control.PreRenderRecursiveInternal() +175
    System.Web.UI.Control.PreRenderRecursiveInternal() +175
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496

    I have searched but didn't find anything. Please help me on this.

    Sunday, March 23, 2014 1:14 PM
  • User-330204900 posted

    I think you are missing the set metatable.

    gvTest.SetMetaTable(table);

    Sunday, March 23, 2014 3:12 PM
  • User-389597101 posted

    Hi

    Gridview doesn't contain the definition of SetMetaTable method.

    Sunday, March 23, 2014 9:41 PM
  • User-389597101 posted

    Hi

    One more step :). I have inherited the class with INamingContainer and found the method SetMetaData(). So I have changed the code to the following

    public partial class _Default : System.Web.UI.Page, INamingContainer
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            gvTest.EnableDynamicData(typeof(Class));
            gvTest.SetMetaTable(EntityDataSource1.GetTable());
        }
    
    }
    

    But still I am getting the same error.

    Monday, March 24, 2014 1:57 PM
  • User-330204900 posted

    the ASP.Net GridView already implements INamingContainer, is you grid the asp.net Gridview if not I woudl not expect it to work.

    Monday, March 24, 2014 6:42 PM
  • User-389597101 posted

    Hi

    I have tried using SetMetaTable method without using INamingContainer but I didn't find the method. But once I have inherited the class I can use the method. In the sample project I am using asp.net grid view.

    Monday, March 24, 2014 9:44 PM
  • User-330204900 posted

    try watching this Create rich, data-driven Web apps with ASP.NET 4.5 Web Forms from Build 2011 where i think Damien Edward demonstrates Model Binding with GridView.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 25, 2014 9:42 AM