shamim
shamim

Reputation: 6768

What's problem on linq databinding

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" 
            KeyFieldName="CategoryID">
   <SettingsEditing Mode="Inline" />
   <Columns>
      <dx:GridViewCommandColumn VisibleIndex="0">
         <EditButton Visible="True"></EditButton>
         <NewButton Visible="True"></NewButton>
         <DeleteButton Visible="True"></DeleteButton>
      </dx:GridViewCommandColumn>
      <dx:GridViewDataTextColumn Caption="CategoryID" FieldName="CategoryID" 
                    VisibleIndex="1">
      </dx:GridViewDataTextColumn>
      <dx:GridViewDataTextColumn Caption="CategoryName" FieldName="CategoryName" 
                    VisibleIndex="2">
      </dx:GridViewDataTextColumn>
      <dx:GridViewDataTextColumn Caption="Description" FieldName="Description" 
                    VisibleIndex="3">
      </dx:GridViewDataTextColumn>
   </Columns>
</dx:ASPxGridView>

C# syntax:

NorthwindDataContext db = new NorthwindDataContext();
var lresult = (db.Categories
                .Select(p => new { p.CategoryID, p.CategoryName, p.Description}));           
ASPxGridView1.DataSource = lresult;
ASPxGridView1.DataBind();

If you run the code, you get a gridview which is filled by NorthWind Categories table. If you click on command button of grid whose are on left side, you get insert/update field, but you have not access to give input. They are gone to read only mode.

If I replace the above C# syntax with below

NorthwindDataContext db = new NorthwindDataContext();
var lresult = (db.Categories);        
ASPxGridView1.DataSource = lresult;
ASPxGridView1.DataBind();

then it works fine. Now you can work with command button with out facing any problem.

I want to know what the problem is, why the first syntax does not work. Maybe you say Anonymous types are class types that consist of one or more public read-only properties. But when you need to join more than one table and need to select several fields not all than what you do. Hope you not say linq is fail to do that or Don't think it is possible. Hope there must be any technique or else something to bind control with Anonymous type. Plz show some syntax .

Upvotes: 0

Views: 1149

Answers (3)

tbmihailov
tbmihailov

Reputation: 161

The problem is that the result set is collection of Anonymous type as you supposed and the grid doesn't know how to treat it. What you have to do is to use RowInserting and RowUpdating events of the grid. Here is an example of how I use DevExpress grid with NHibernate:

protected void gridAgentGroups_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        ASPxGridView currentGrid = sender as ASPxGridView;

        var currentAgentGroup = new AgentGroup();
        if (e.NewValues.Contains("Name"))
        {
            var newValue = (string)e.NewValues["Name"];
            currentAgentGroup.Name = newValue;
        }
        if (e.NewValues.Contains("PhysicalAddress"))
        {
            var newValue = (string)e.NewValues["PhysicalAddress"];
            currentAgentGroup.PhysicalAddress = newValue;
        }

        AgentGroupsDataAccess.SaveAgentGroup(currentAgentGroup);

        e.Cancel = true;
        currentGrid.CancelEdit();
        currentGrid.DataBind();

    }

    protected void gridAgentGroups_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        ASPxGridView currentGrid = sender as ASPxGridView;

        int currentAgentGroupId = (int)((AgentGroup)currentGrid.GetRow(currentGrid.EditingRowVisibleIndex)).Id;
        var currentAgentGroup = AgentGroups.Where(ag => ag.Id == currentAgentGroupId).FirstOrDefault();

        if (e.NewValues.Contains("Name"))
        {
            var newValue = (string)e.NewValues["Name"];
            currentAgentGroup.Name = newValue;
        }
        if (e.NewValues.Contains("PhysicalAddress"))
        {
            var newValue = (string)e.NewValues["PhysicalAddress"];
            currentAgentGroup.PhysicalAddress = newValue;
        }

        AgentGroupsDataAccess.SaveAgentGroup(currentAgentGroup);

        e.Cancel = true;
        currentGrid.CancelEdit();
        currentGrid.DataBind();
    }

I hope this will help.

Upvotes: 2

Sascha
Sascha

Reputation: 10347

You actually can bind with anonymous type as you see the already filled rows. But: the grid itself cannot know how you build the query and what to add additionally to the visible columns (if there are valid default values).

As you use Developer Express' grid you have the option to provide your own update / edit form and handle everything needed on your own.

Upvotes: 0

marc_s
marc_s

Reputation: 754488

Just a wild guess - you're binding your data to the grid using field names - yet, your anonymous type doesn't really have any field names.

Does it make any difference if you try this code:

NorthwindDataContext db = new NorthwindDataContext();

var lresult = (db.Categories
               .Select(p => new { CategoryID = p.CategoryID, 
                                  CategoryName = p.CategoryName, 
                                  Description = p.Description}));           

ASPxGridView1.DataSource = lresult;
ASPxGridView1.DataBind();

Again - I don't have the means to test this right now, it's just a gut feeling..... try it - does that help at all??

Upvotes: 0

Related Questions