|
How to hide pager of a DataGrid Web Control
A nice feature of DataGrid web control is that in stead of showing all the
records on one page it lets you set AllowPaging property to page
through the records. And you can set the page size of the grid by specifying PageSize
property. A lot of time the data source that grid is bound to has records less
than the page size. Since you have already set AllowPaging property,
the pager gets rendered at the bottom of the grid showing "1". If you want to
get rid of pger if its not needed, you have 2 choices.
-
When you get the data for the grid, check the record count. If number of rows
is less than or equal to page size, then set
AllowPaging property
to false.
bool m_bShowPager = false;
private void GetData()
{
sqlDataAdapter1.Fill(m_dsProducts);
// Check if we have data for more than one page.
m_bShowPager = (m_dsProducts.Tables[0].Rows.Count > ProdutsDataGrid.PageSize);
ProdutsDataGrid.AllowPaging = m_bShowPager;
}
-
Second technique is by making use of
ItemCreated event handler of
data grid. When you get data for the grid, set a class variable that indicates
if pager needs to be shown or not. If this value is set to false, then you can
set the Visibility of pager item to false in ItemCreated event
handler.
private void OnGridItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Pager)
{
if (!this.m_bShowPager)
{
e.Item.Visible = false;
}
}
}
|