Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / June 2005

Tip: Looking for answers? Try searching our database.

Need help with DataGrid and Pager.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christian-Josef Schrattenthaler - 14 Jun 2005 18:54 GMT
Hi!

I have a working website which gets data from an access-files over oledb and
the .Fill() method. The data is sent and bound to a table, which is
displayed on the website.

But the list is verry long, and now I want to make a Paging.

I searched on the Internet, and tried some samples. The best one I did find
was
http://samples.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl
/datagrid/doc_datagrid.aspx
,
but I didn't get it to work...

There is no data, or the only first 10 rows, and the < > symbols or numbers,
but I can't klick on the symbols or numbers.

Please, is there no working sample which I can use to understand?

Or can anyone give me hints what I have to do?

Greetings,
christian.
Lars-Inge Tønnessen [VJ# MVP] - 15 Jun 2005 20:04 GMT
In my example I'm using a MS SQL server. Please change the
"System.Data.SqlClient." namespace to your "System.Data.OleDb." namespace.

Open Visual Studio and open a new J# ASP.NET WebApplication. Add a DataGrid
WebForms control to the web page in design mode.

Right click the control and choose "Properties". Enable paging by choosing
"True" on the property "AllowPaging". You can also set the page size on the
"PageSize" property.

Limitations
Important: You can only use paging for a data sources that implements the
ICollection interface. We will be using a System.Data.DataTable in this
example because it implements this interface. A DataReader does not
implement the ICollection interface.

The datasource
In this example we will show data from a MS SQL database. If the page
request is not a postback, do a database lookup.

     private void Page_Load(Object sender, System.EventArgs e)
     {
           if ( !this.get_IsPostBack() )
           {
                 // Initialize the DataGrid
                 this.InitializeDataGrid();
           }
     }

     private void InitializeDataGrid( )
     {
           try
           {
                 // DB connection string
                 String _connectionString = "Data
Source=localhost\\MinDB;Initial Catalog=BMWCCN;User
Id=aspnet;Password=aspnet";
                 // Open a connection to the database using SQL login
                 System.Data.SqlClient.SqlConnection _connection = new
System.Data.SqlClient.SqlConnection( _connectionString );

                 // The sql statement
                 String _sql = "SELECT * FROM Forum ORDER BY MeldingsId";

                 // Creates a DataTable for the sql result.
                 System.Data.DataTable _dataTable = new
System.Data.DataTable( );

                 // Uses a DataAdapter to get the data from the database.
                 System.Data.SqlClient.SqlDataAdapter _adapter = new
System.Data.SqlClient.SqlDataAdapter( _sql, _connection );
                 _adapter.Fill( _dataTable );

                 // Sets the datasource and binds it to the DataGrid.
                 this.DataGrid1.set_DataSource( _dataTable );
                 this.DataGrid1.DataBind();

           }
           catch ( Exception ex )
           {
                 this.get_Trace().Warn( ex.getMessage() );
           }
     }

PageIndexHander
Add a "Page index changed" event handler to the control. Open the design
view. Right click it and choose "Properties". Click on the lightning icon in
the properties window to show all event handlers. Double click the
"PageIndexChanged" text. Visual Studio will add a handler to your code.

In the index page handler you will tell the control what data your control
should show.

     private void DataGrid1_PageIndexChanged (Object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
     {
           this.DataGrid1.set_CurrentPageIndex( e.get_NewPageIndex() );
           this.InitializeDataGrid( );
     }

This should be all there is to simple paging. You can go forward or
backbords with the "<" and the ">".

If you want numbers instead of the "<" and the ">" to navigate in the pages,
please do:

Switch to design mode in Visual Studio. Right click the control and choose
"Properties". Under "Styles" locate the "PageStyle". Expand it and find the
"Mode". Switch from NextPrev" to "NumericPages".

Best regards,
Lars-Inge Tønnessen
Lars-Inge Tønnessen [VJ# MVP] - 15 Jun 2005 20:37 GMT
I have written a short introduction article about it here:

http://www.codeproject.com/useritems/DataGridPagingJ.asp

Best Regards,
Lars-Inge Tønnessen

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.