all groups > vj# > june 2005 >
You're in the

vj#

group:

Need help with DataGrid and Pager.


Need help with DataGrid and Pager. Christian-Josef Schrattenthaler
6/14/2005 7:54:30 PM
vj#:
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.

Re: Need help with DataGrid and Pager. Lars-Inge Tønnessen [VJ# MVP]
6/15/2005 9:04:11 PM
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

Re: Need help with DataGrid and Pager. Lars-Inge Tønnessen [VJ# MVP]
6/15/2005 9:37:13 PM

I have written a short introduction article about it here:

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


Best Regards,
Lars-Inge Tønnessen

AddThis Social Bookmark Button