Library tutorials & articles

Web Forms DataGrid and DataSet Programming

Introduction

This is a working C# .NET program that demonstrates how to integrate most of the features of the DataGrid and DataSet in a single project including select, insert, update, delete, confirm delete, sort, filter and page. This is a work in progress and feedback is an important factor in improving this tutorial. Please report any bugs and I will try to fix them.

A few general concepts are presented here and represent my best understanding of how the DataSet and DataGrid works.

The SqlDataAdapter provides a bridge between the DataSet and the data source. Calling Fill, refreshes the data in the DataSet. Calling Update, attempts to commit any changes in the DataSet. The SqlDataAdapter wizard simplifies building of data driven forms by automatically generating UPDATE, INSERT and DELETE statements from your SqlDataAdapter SELECT statement. By default, the UPDATE statement implements an Optimistic Concurrency scheme that looks for an unaltered row on UPDATE.

The DataSet can encapsulate a parent child hierarchy of zero or more data tables. The DataSet also stores its state, storing both the @original values and any new uncommitted values. These original values are used to enforce "Optimistic Concurrency" by searching for unaltered rows on UPDATE. Unlike the DataGrid, the DataSet does _not_ persist itself on post back.

The DataGrid is used to display data in the HTML page. If DataGrid paging is not enabled, the DataGrid will display all of the rows in the appropriate table in the corresponding DataSet. To enable sorting and filtering, the DataGrid is bound to a View, not to the DataSet.

The View is used to sort and filter data in the DataSet before it is passed to the DataGrid using the properties:

view.RowFilter
view.Sort

The data path looks like this:

DataBase --> DataSet --> View --> DataGrid

Comments

  1. 26 Jan 2006 at 14:50
    How could i do all this with untyped dataset from xml source? (no find methods etc..)
    Vjero
  2. 23 Nov 2005 at 14:26

    hi
    hey ppl i m   inserting some data manually in a dataview then i m sorting that dta via dataview.sort , sorting is taking place perfectly here but when i embed this data into a word document  then  i found that data is not  in sorted order as this supposed to be but it is in order in which it had typed...........so guys if u  have have any solution of this problem  then plz  mail me at aman_105@rediffmail.com

  3. 10 Nov 2005 at 09:33

    hello
     I am converting C#.net code into vb.net code .
     Sorting ASC or DESC  is not working .
     while degugging ,code work fine but i think some how dataset is not getting refreshed.
     I have commented these line  'view.RowFilter = lastFilter' .Is it require for sorting
     Execpt that everything is same
      please help me out .
                                                Thanks            
     

  4. 30 Jun 2005 at 02:13

    Hi.


    I dont know if this only a problem of mine, but when I hit a sort column the DataGridSortCommand happens twice.


    Should this happen? In my case it happens and so all the code in that event doesnt work properly...


    Could some help me out on this one?


    TIA,
    C.C.

  5. 10 Nov 2004 at 09:24
    How do I obtain ResetPageIndex? It is unknown to my installation of .NET or at least to the libraries I am using.
  6. 01 May 2004 at 12:13

    Jeff,


    Thanks a bunch!  I am new to ASP.NET programming and it was having a devil of a time getting my datagrid to sort -- your example provided the one missing link: the DataSource property should have been set to the view and not the dataset! Tanx again.


    --Babak

  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Web Forms DataGrid and DataSet Programming.

Leave a comment

Sign in or Join us (it's free).

Jeff Louie

Related podcasts

Events coming up

  • Mar 25

    Hyper-V For Developers with Liam Westley

    Bristol, United Kingdom

    Liam Westley returns to Bristol fresh from his DDD8 exploits to give us a second dose of virtualization, this time focussing on Hyper-V.

We'd love to hear what you think! Submit ideas or give us feedback