GridView: How to set the number of rows to display
Asked Answered
L

7

13

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

Thanks

Lune answered 19/4, 2012 at 11:39 Comment(1)
you want only 3 rows to be added or you want to set the size of the grid based on row heightHerpes
C
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)).

Curare answered 19/4, 2012 at 11:43 Comment(1)
I'd missed AllowPaging="true" and `AllowSorting="true"`` on my GridView. Thanks a bunch :)Oh
T
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.

Took answered 19/6, 2012 at 13:42 Comment(0)
C
2

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

Caftan answered 2/6, 2017 at 9:14 Comment(0)
T
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].

Thoroughwort answered 19/4, 2012 at 11:45 Comment(0)
W
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!!!!!!

Wunder answered 19/4, 2012 at 11:46 Comment(0)
U
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

Unquestioning answered 20/9, 2013 at 0:33 Comment(0)
E
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>
Evars answered 27/5, 2017 at 9:57 Comment(1)
It looks there is a code formatting issue. Please fix it.Outgrow

© 2022 - 2024 — McMap. All rights reserved.