DataBinding in VS2005 w/GridView

Posts   
 
    
Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 14-Nov-2005 08:43:27   

I am trying to do a simple databinding of my ShoppingCartItemsCollection to the VS 2005 Gridview. I would like for it to bind with the support for paging and sorting. Is this functionality currently available in the latest release? Can I drag the collection to the asp.net page and then bind to it? Can I assign it to an objectDataSource? If anyone has this figured out can you share some code and the steps to make it work?

Thanks, Wade Beasley

Walaa avatar
Walaa
Support Team
Posts: 14951
Joined: 21-Aug-2005
# Posted on: 14-Nov-2005 15:29:43   

Please refer to the folllowing section in the LLBLGen Pro v.1.0.2005.1 documentation

"Design time support in VS.NET 2005" under "Generated code -> Adapter/SelfServicing -> Databinding"

Please get back to us if this didn't address your question. Good Luck

SeanC
User
Posts: 9
Joined: 13-Mar-2005
# Posted on: 17-Nov-2005 20:36:42   

There are a lot of threads asking about this and I had a heck of a time getting this to work with the ObjectDataSource. It isn't readily apparent from any documentation, but what the key to making all of the binding to EntityCollection work is to set the DataKeyNames attribute on the GridView to your Entity's PK value. This gets you the ability to do deletes and updates. That's the hard part. The easy part is binding to an EntityCollection. It looks ugly and design time, but works at run-time. Here's an example of using a GridView w/ ObjectDataSource to do ADD, UPDATE, DELETE operations...


<asp:GridView ID="gridViewManageGroups" runat="server" AllowPaging="True" ShowFooter="true"
        AutoGenerateColumns="False" DataKeyNames="GroupID" DataSourceID="ObjectDataSourceGroup">
        <EmptyDataTemplate>
                No data found.
            </EmptyDataTemplate>
        <Columns>
                <asp:BoundField DataField="GroupId"  />
                <asp:BoundField DataField="OrganizationId"  />
                <asp:BoundField DataField="GroupName" HeaderText="Group Name" />
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:ImageButton id="imageButtonEdit" runat="server" CommandName="Edit" ImageUrl="~/images/icons/ondark/edit.gif" AlternateText="Edit Record" />
                        <asp:ImageButton id="imageButtonDelete" runat="server" CommandName="Delete" ImageUrl="~/images/icons/ondark/x_small.gif" AlternateText="Delete Record" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton id="imageButtonUpdate" runat="server" CommandName="Update" ImageUrl="~/images/icons/ondark/disk.gif" AlternateText="Save" />
                        <asp:ImageButton id="imageButtonCancel" runat="server" CommandName="Cancel" ImageUrl="~/images/icons/onlight/cancel.gif" AlternateText="Cancel" />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="textBoxNewGroup" runat="server" MaxLength="50"></asp:TextBox>
                        <asp:ImageButton id="imageButtonSaveNew" runat="server" OnClick="imageButtonNewGroup_Click" CommandName="Insert" ImageUrl="~/images/icons/ondark/disk.gif" AlternateText="Save" />
                    </FooterTemplate>
                    <ItemStyle Width="100px" />
                </asp:TemplateField>
            </Columns>
            <FooterStyle CssClass="gridViewFooter" />
                    <RowStyle CssClass="gridViewRow" />
                    <HeaderStyle CssClass="gridViewHeader" />
                    <AlternatingRowStyle CssClass="gridViewAlternateRow" />
    </asp:GridView>
    <br />
    <asp:ObjectDataSource ID="ObjectDataSourceGroup" runat="server"
            SelectMethod="GetGroups" TypeName="App.Web.Stuff.Group"
            DeleteMethod="DeleteGroup" UpdateMethod="UpdateGroup" InsertMethod="InsertGroup"
             OnDeleted="ObjectDataSourceGroup_Deleted" OnInserted="ObjectDataSourceGroup_Inserted" OnUpdated="ObjectDataSourceGroup_Updated" DataObjectTypeName="App.Common.EntityClasses.GroupEntity">
            <InsertParameters>
                <asp:Parameter Name="groupName" Type="String" DefaultValue="" />
            </InsertParameters>
        </asp:ObjectDataSource>

The class (App.Web.Stuff.Group.cs) in my APP_CODE that I bind the ObjectDataSource control to looks something like this...


/// <summary>
/// Gets the groups.
/// </summary>
/// <returns></returns>
public EntityCollection GetGroups()
{
    //Call into BL and get a collection of groups
    OrganizationManager manager = new OrganizationManager(SessionManager.OrganizationID);
    EntityCollection groups = manager.GetGroups();
    return groups;
}

/// <summary>
/// Creates a new group
/// </summary>
/// <param name="groupName">Name of the group.</param>
public void InsertGroup(string groupName)
{
    //call into BL and create a new group
    OrganizationManager manager = new OrganizationManager(SessionManager.OrganizationID);
    manager.CreateNewGroup(groupName);
}

/// <summary>
/// Updates the group.
/// </summary>
/// <param name="group">The group.</param>
public void UpdateGroup(GroupEntity group)
{
    OrganizationManager manager = new OrganizationManager(SessionManager.OrganizationID);
    //fetch entity from BL
    GroupEntity groupEntity = manager.GetGroup(group.GroupId);

    //set updated values
    groupEntity.OrganizationId = group.OrganizationId;
    groupEntity.GroupName = group.GroupName;
    //call update in the BL
    manager.UpdateGroup(groupEntity);
}

/// <summary>
/// Deletes the group.
/// </summary>
/// <param name="group">The group.</param>
public void DeleteGroup(GroupEntity group)
{
    //call into BL, send in the PK value for the entity to delete
    OrganizationManager manager = new OrganizationManager(SessionManager.OrganizationID);
    manager.DeleteGroup(group.GroupId);
}

Hope that helps,

Sean