views:

108

answers:

3
<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 .

A: 

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

marc_s
I already do it,it's not working.Why not u try to run the above syntax
shamim
A: 

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.

Sascha
I don't understand what u want to say.Will you plz explain it in detail and show me some syntax to solve this problem
shamim
As marc_s already said, anonymous types do no have columnnames to bind to. Also, you'll loose every bit of information how the database handles the data, each data may be from a different table. No single standard software may know how to add data to a database without having access to some basic information.Have a look at http://demos.devexpress.com/ASPxGridViewDemos/GridEditing/EditFormTemplate.aspx. That sample should guide you the way.
Sascha
A: 

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.

Todor Mihailov