C# GridView:如何设置要显示的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10227218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridView: How to set the number of rows to display
提问by Jean Claude Abela
I would like my grid view to display only 3 rows any ideas on how I can achieve this?
我希望我的网格视图只显示 3 行关于如何实现这一目标的任何想法?
Thanks
谢谢
采纳答案by Tim Schmelter
Enable Pagingand set the GridView's PageSizeto 3.
启用分页并将 GridView 设置PageSize为 3。
How to: Enable Default Paging in the GridView Web Server Control
如何:在 GridView Web 服务器控件中启用默认分页
If you want to restrict your GridView to show only 3 rows without paging, you need to use a DataSourcewith only 3 records (f.e. via SQL-TOP-Clause or Limitin MySQL or LINQ's Take(3)).
如果你想限制你的 GridView 只显示 3 行而不显示分页,你需要使用一个DataSource只有 3 条记录(fe 通过 SQL- TOP-Clause 或Limit在 MySQL 或 LINQ's 中Take(3))。
回答by Widor
I'd keep it simple and ensure your DataSource only provides the three rows of data you need to display.
我会保持简单并确保您的 DataSource 仅提供您需要显示的三行数据。
Failing that, you could set the .Visibleproperty of all Rowsto false, except Rows[0]through Rows[2].
否则,您可以将.Visibleall的属性设置Rows为false,除了Rows[0]through Rows[2]。
回答by kingpin
2 ways that i can think of.....
我能想到的2种方法......
- 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....
- 从查询中获取数据集。
- 创建列并添加到您的网格视图...
- 单击按钮添加 3 行并保持索引静态
- 在同一次单击中,清除您的网格并添加接下来的三行....
OR
或者
Use paging!!!!!!
使用分页!!!!!!
回答by Eric Burdo
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;}
- 设置“
allowpaging=true”和“pagesize=X”(将 X 更改为您希望可见的行数)。 使用自定义 CSS 类分配 pagerstyle。
<pagerstyle cssclass="隐藏" />
将该自定义类设置为:
.hidden { 可见性:隐藏;显示:无;}
Now, your grid will use the paging logic, but the pager controls are hidden.
现在,您的网格将使用分页逻辑,但分页控件是隐藏的。
It's not the cleanest/most elegant, but it works.
它不是最干净/最优雅的,但它有效。
回答by user529481
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
转到查看并单击网格,然后会打开一个小叠加层,允许(需要您)为该列输入一个数字。然后预览并点击保存
回答by Hasan Zafari
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")%>' >
<%#If((Container.ItemIndex <> 0 AndAlso Container.ItemIndex Mod 4 = 0), " ", String.Empty)%> ' PostBackUrl='<%# Container.DataItem("url")%>' >
</asp:Repeater>
回答by user8102357
place AllowPaging="True" and PageSize="3" in GridView
在 GridView 中放置 AllowPaging="True" 和 PageSize="3"

