Binding an ASP.NET 2.0 GridView to a DataSet stored in session


I just want to share some experience about binding the ASP.NET 2.0 GridView (DataGrid successor) to a DataSet without using the DataAdapter like feature of the datasource.


There are two ways to bind the GridView: DataSourceID (basically used at design time) that binds to a dataSource control or DataSource (basically used at runtime). I was in a case where the DataSet was stored in session. The code uses adapters to fill it and update it, but I did not want the gridview datasource to do it (update could occur after several screens interactions).


I first tried the DataSource approach and it seems that it is the wrong way to go: The GridVoew could show the DataSet, but trying to update it quickly became a nightmare with more and more useless code 🙁


Then I used the DataSourceID. It points to an objectDataSource and I created such an object. Here is how it looks:


The page has a GridView with a DataSourceID pointing to an ObjectDataSource control pointing to a small class which handles the events

<%@ Page Language="C#" MasterPageFile="~/Main.master"
AutoEventWireup="true" CodeFile="TwoGridUI.aspx.cs"
Inherits="TwoGridUI" Title="Untitled Page" %>
<asp:Content ID="Content1"
ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" DataKeyNames="inv_ID"
DataSourceID="RuntimeObjectDataSource">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="inv_ID" HeaderText="ID" ReadOnly="True" SortExpression="inv_ID" />
            <asp:BoundField DataField="inv_CreationDate" HeaderText="CreationDate" ReadOnly="True"
                SortExpression="inv_CreationDate" />
            <asp:BoundField DataField="inv_TotalAmount"
HeaderText="inv_TotalAmount" SortExpression="TotalAmount" />
        </Columns>
    </asp:GridView>
    &nbsp;<asp:ObjectDataSource ID="RuntimeObjectDataSource" runat="server" DeleteMethod="Delete"
        InsertMethod="Insert" SelectMethod="Select" TypeName="InvoiceDataSource"
        UpdateMethod="Update">
    </asp:ObjectDataSource>
    &nbsp; &nbsp;&nbsp;
    <br />
</asp:Content>

Here is the class the object DataSource uses

using System;
using System.Data;
using System.Configuration;
using System.Diagnostics;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Contoso.Architecture;
using Contoso.SampleApplication.BusinessEntity;

///


/// Summary description for InvoiceDataSource
///

public class InvoiceDataSource
{
private DataSetArtifacts.inv_InvoiceDataTable InvoiceTable
{
get
{
return (DataSetArtifacts.inv_InvoiceDataTable)
ContosoContext.Current.DataContexts["invoice"].DataSet.Tables["inv_Invoice"];
}
}

public DataSetArtifacts.inv_InvoiceDataTable Select()
{
return InvoiceTable;
}

public void Update(double inv_TotalAmount, int original_inv_ID)
{
DataSetArtifacts.inv_InvoiceRow row = InvoiceTable.FindByinv_ID(original_inv_ID);
row["inv_TotalAmount"] = inv_TotalAmount;
}

public void Delete(int original_inv_ID)
{
DataSetArtifacts.inv_InvoiceRow row = InvoiceTable.FindByinv_ID(original_inv_ID);
row.Delete();
Debug.WriteLine(row.RowState);
}

public void Insert(int inv_Id, DateTime inv_CreationDateTime, double inv_TotalAmount)
{
System.Diagnostics.Debug.WriteLine(string.Format(
"{0} {1} {2}", inv_Id, inv_CreationDateTime, inv_TotalAmount));
}
}


The beauty of this is the code behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Diagnostics;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TwoGridUI : SampleApplicationPageController
{
protected void Page_Load(object sender, EventArgs e)
{
MaintainScrollPositionOnPostBack = true;
}
}

Skip to main content