locked
How to bind a WCF Data Service function call to an ASP.NET GridView control ? RRS feed

  • Question

  • How do I bind data returned from a WCF Data Service function call to an ASP.NET GridView control?

    My WCF Data Service function call returns a complex type with numerous columns and one row of data. However, when binding it to an ASP.NET GridView control, the GridView just displays a header called "Count" with a value of "1".

    <asp:GridView ID="gridView1" runat="server"></asp:GridView>

    protected void Page_Load(object sender, EventArgs e) { MyEntities entities = new MyEntities(new Uri("http://localhost/MyService.svc/"));

    // http://localhost/MyService.svc/?Param1='val1'&Param2='val2'

    var query = entities.CreateQuery<ObservableCollection<MyFunction_Result>>("MyFunction") .AddQueryOption("Param1", "val1") .AddQueryOption("Param2", "val2");

    gridView1.DataSource = query.ToList(); gridView1.DataBind(); }

     

    Instead of binding one row of data to the GridView with multiple columns; the GridView just shows a single column called "Count" with a value of "1" as seen in the following image...


    The debugger at run-time is clearly showing one row of data with multiple columns as seen in the following image...


    So why aren't the actual contents of the single data row and corresponding multiple column headers not appearing in the GridView control? Any assistance would be appreciated.

     

    Tuesday, July 4, 2017 7:56 AM

All replies

  • Hi 000ABC,

    Based on your description and related image, it seems that the variable named query have sub item. try to modify your modify like below and check if it works for you.

    protected void Page_Load(object sender, EventArgs e)
    {
        MyEntities entities = new MyEntities(new Uri("http://localhost/MyService.svc/"));    
        // http://localhost/MyService.svc/?Param1='val1'&Param2='val2'
    
        List<MyFunction_Result> query = entities.CreateQuery<ObservableCollection<MyFunction_Result>>("MyFunction")
            .AddQueryOption("Param1", "val1")
            .AddQueryOption("Param2", "val2")
            .ToList();
    
        gridView1.DataSource = query;
        gridView1.DataBind();
    }
    

    If the issue still exists, could you please share a simple demo (WCF data service project) about the issue via OneDrive. we'll try my best to find a solution to resolve the issue.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, July 12, 2017 7:30 AM
    Moderator
  • Thank you for the response.

    However, your suggested code sample still returns "Count=1" in the GridView control as follows...

    This issue is not resolved.

    Friday, July 14, 2017 1:27 PM