Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView: How to set the number of rows to display

Tags:

c#

gridview

rows

I would like my grid view to display only 3 rows any ideas on how I can achieve this?

Thanks

like image 837
Jean Claude Abela Avatar asked Apr 19 '12 11:04

Jean Claude Abela


People also ask

How display total number of records in GridView C#?

In order to implement Paging in GridView, AllowPaging property is set to true and OnPageIndexChanging event has been handled. Below the GridView, there is a Label control which will be used to display the Count of Total number of records in the GridView. You will need to import the following namespaces.

How do I get the number of rows in C#?

Use Property of RowCount to get the number of rows in a Data Grid View.

How do I add pagination to GridView?

To enable the paging feature, set the AllowPaging property to true . By default, the GridView control displays 10 records on a page at a time. You can change the number of records displayed on a page by setting the PageSize property.

Which property of GridView should be set in order to change the number of records per page?

PageSize is the property that can be used to set the number of records per page.


2 Answers

Enable Paging and set the GridView's PageSize to 3.

How to: Enable Default Paging in the GridView Web Server Control

If you want to restrict your GridView to show only 3 rows without paging, you need to use a DataSource with only 3 records (f.e. via SQL-TOP-Clause or Limit in MySQL or LINQ's Take(3)).

like image 166
Tim Schmelter Avatar answered Oct 19 '22 07:10

Tim Schmelter


If you can limit the records in your query, then that's the best approach.

However, if you can't limit them in the query... here is another approach:

  1. Set "allowpaging=true" and "pagesize=X" (change X to how many rows you want visible).
  2. Assign a pagerstyle with a custom CSS class.

    <pagerstyle cssclass="hidden" />

  3. Set that custom class to:

    .hidden { visibility: hidden; display: none; }

Now, your grid will use the paging logic, but the pager controls are hidden.

It's not the cleanest/most elegant, but it works.

like image 38
Eric Burdo Avatar answered Oct 19 '22 07:10

Eric Burdo