问题
I would like my grid view to display only 3 rows any ideas on how I can achieve this?
Thanks
回答1:
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)
).
回答2:
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:
- Set "
allowpaging=true
" and "pagesize=X
" (change X to how many rows you want visible). Assign a pagerstyle with a custom CSS class.
<pagerstyle cssclass="hidden" />
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.
回答3:
place AllowPaging="True" and PageSize="3" in GridView
回答4:
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]
.
回答5:
2 ways that i can think of.....
- Get your dataset from your query.
- Create columns and add to your gridview...
- Add 3 rows on a button click and keep the index static
- On the same click, clear your grid and add next three rows....
OR
Use paging!!!!!!
回答6:
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
回答7:
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>
来源:https://stackoverflow.com/questions/10227218/gridview-how-to-set-the-number-of-rows-to-display