Monday, August 16, 2010

Gridview paging in asp.net?

i m working on this page with gridview which i flood from my vb code, same adap.fill, gridview.databind(). now i want to page this gridview. while this code http://dotnetjunkies.com/Tutorial/AAF84EAD-C412-4304-A88A-AF26F8C883E6.dcik


works for a datagrid. i want similar functionality for a gridview. can anyone help??? please its urgent.. my deadline is tom n i've wasted enough time. as in what is the code that goes in pageindexchanged... also if you could type a tiny code showing how rowcommand would work if i were to access the property of an object in my gridview... eg. activate button text to deactivate on clicking....Gridview paging in asp.net?
The GridView is NOT a DataGrid. If you are using a GridView, then achieving paging is even easier than in a DataGrid. Below I give the code for a page that uses Paging in a GridView.





I write the whole code inline (in aspx file) to simplify stuff.





%26lt;%@ Page Language=';C#'; AutoEventWireup=';true'; %%26gt;


%26lt;%@ Import namespace=';System.Data';%%26gt;


%26lt;%@ Import namespace=';System.Data.SqlClient';%%26gt;


%26lt;html %26gt;


%26lt;head runat=';server';%26gt;


%26lt;title%26gt;Untitled Page%26lt;/title%26gt;


%26lt;script runat=';server';%26gt;


protected void Page_Load(object sender, EventArgs e)


{


if (!Page.IsPostBack)


{


BindGrid();


}


}





private void BindGrid()


{


SqlConnection MyConnection;


SqlCommand MyCommand;





MyConnection = new SqlConnection(';server=.;DataBase=Northwi鈥?br>

MyCommand = new SqlCommand(';SELECT * FROM CUSTOMERS ';, MyConnection);





DataTable dt = new DataTable();


SqlDataAdapter da = new SqlDataAdapter(MyCommand);


MyCommand.Connection.Open();


da.Fill(dt);


MyCommand.Dispose();


MyConnection.Dispose();





gvwTest.DataSource = dt;


gvwTest.DataBind();





}





protected void gvwTest_PageIndexChanging(object sender, GridViewPageEventArgs e)


{


BindGrid();


gvwTest.PageIndex = e.NewPageIndex;


gvwTest.DataBind();


}


%26lt;/script%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;form id=';form1'; runat=';server';%26gt;


%26lt;asp:GridView ID=';gvwTest'; runat=';server'; AutoGenerateColumns=';true'; AllowPaging=';true'; PageSize=';10';


OnPageIndexChanging=';gvwTest_PageIndex鈥? /%26gt;


%26lt;/form%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





Remember.. look for gridView code snippets not DataGrid!!


Hope the above helps.

No comments:

Post a Comment