here is an example through which you can show grid with paging..
Below code is for aspx page:
<asp:DataGrid ID="dgadvetise" runat="server" AutoGenerateColumns="False"
onpageindexchanged="dgadvetise_PageIndexChanged"
PageSize="5" AllowPaging="True" AllowSorting="True" DataKeyField="ID">
<PagerStyle HorizontalAlign="Center" Mode="NumericPages"
Position="TopAndBottom"/>
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="Image ID" Visible="False" />
<asp:BoundColumn DataField="Name" HeaderText="Name" />
<asp:BoundColumn DataField="Link" HeaderText="Link" />
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<ul class="hoverbox">
<li>
<a href="#"><img id="imgSmall" src='Picture/<%# DataBinder.Eval(Container, "DataItem.ImagePath") %>' alt="Gallery Image" width="100" height="100" /></a>
</li>
</ul>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="flUploadGrid" runat="server" ForeColor="#2A2F0F"></asp:FileUpload>
</EditItemTemplate>
<HeaderStyle Width="110px" />
</asp:TemplateColumn>
<asp:EditCommandColumn CancelText="Cancel" EditText="Edit" HeaderText="Modify" UpdateText="Update"/>
<asp:ButtonColumn CommandName="Delete" Text="Delete" HeaderText="Remove"/>
</Columns>
</asp:DataGrid>
<asp:SqlDataSource ID="nayumaadminconnectionstring" runat="server"
ConnectionString="<%$ ConnectionStrings:nayumaConnectionString %>"
SelectCommand="SELECT * FROM [Details]"></asp:SqlDataSource>
And the cs page code is:
protected void dgadvetise_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dgadvetise.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void BindGrid()
{
cls.PopulateGrid(dgadvetise, "Select ID, Name, Link, ImagePath from Details order by ID", this.Page);
}
I am using a common class which is mentioned below:
public void PopulateGrid(GridView grdView, string sqlQuery, Page form)
{
try
{
adp = new SqlDataAdapter(sqlQuery, conn);
tbl = new DataTable();
adp.Fill(tbl);
grdView.DataSource = tbl;
grdView.DataBind();
}
catch (Exception ex)
{
MsgBox("Populate GridView Error: " + ex.Message, form);
}
}
NB: specify your connection in the class file.
I hope this above code will help you.