13

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

Thanks

Jean Claude Abela
  • 782
  • 1
  • 9
  • 26

7 Answers7

20

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)).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I'd missed `AllowPaging="true"` and `AllowSorting="true"`` on my GridView. Thanks a bunch :) – Ortund Aug 17 '17 at 12:49
8

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.

Eric Burdo
  • 812
  • 1
  • 10
  • 24
2

place AllowPaging="True" and PageSize="3" in GridView

0

I'd keep it simple and ensure your DataSource only provides the three rows of data you need to display.

Failing that, you could set the .Visible property of all Rows to false, except Rows[0] through Rows[2].

Widor
  • 13,003
  • 7
  • 42
  • 64
0

2 ways that i can think of.....

  1. Get your dataset from your query.
  2. Create columns and add to your gridview...
  3. Add 3 rows on a button click and keep the index static
  4. On the same click, clear your grid and add next three rows....

OR

Use paging!!!!!!

kingpin
  • 83
  • 3
  • 12
0

go to view and click on grid and a small overlay opens allowing (requiring you) to enter a number for the column. then preview and click save

0

you can use Repeater instead as follow.

<asp:Repeater ID="Repeater2" runat="server" >
<HeaderTemplate>
<table class="center">
    <tr>

<%#If((Container.ItemIndex <> 0 AndAlso Container.ItemIndex Mod 4 = 0), " ", String.Empty)%> ' PostBackUrl='<%# Container.DataItem("url")%>' >

</asp:Repeater>
Hasan Zafari
  • 355
  • 2
  • 6