GridView固定行以后的翻页问题

萬里無雲 2008-05-09 01:49:10
GridView进行翻页没有问题,但是因为需要当页面数据不满18时补空行到18行,从网上找了代码过来是可以了,
但是翻页发生问题了,比如有2页,点前一页的时候,显示的还是刚才那页的内容,而且补的空白行也没有了,
再点一次才能正常回到第一页。代码如下,帮忙看看啊,谢谢谢谢!

<asp:GridView ID="gvProgram" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="FileDataSource" OnDataBound="gvProgram_DataBind"
OnPageIndexChanging="gvProgram_PageIndexChanging" OnRowCommand="gvProgram_RowCommand"
Height="498px" Width="531px" PageSize="18" OnRowDataBound="gvProgram_RowDataBound"
>
<PagerTemplate>
<table width="100%">
<tr>
<td>
<asp:ImageButton ID="imageFirst" CommandName="Page" CommandArgument="First" runat="server" />
<asp:ImageButton ID="imagePrevious" CommandName="Page" CommandArgument="Prev" runat="server"/> 
<asp:ImageButton ID="imageNext" CommandName="Page" CommandArgument="Next" runat="server" />
<asp:ImageButton ID="imageLast" CommandName="Page" CommandArgument="Last" runat="server"/>
</td>
</tr>
</table>
</PagerTemplate>
<Columns>
<asp:BoundField DataField="FileName" HeaderText="标题" >
</asp:BoundField>
<asp:BoundField DataField="FileSize" HeaderText="文件大小">
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="FileDataSource" runat="server"
SelectMethod="GetListByFileTypeAndHost" TypeName="BLLFile">
<SelectParameters>
<asp:Parameter DefaultValue="02" Name="fileType" Type="String" />
<asp:Parameter DefaultValue="00" Name="host" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>



protected void gvProgram_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 计算自动填充的行数
numCount++;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
// 计算完毕,在此添加缺少的行
int toLeft = this.gvProgram.PageSize - numCount;
int numCols = this.gvProgram.Rows[0].Cells.Count;
DataControlRowState rowState = (this.gvProgram.Rows[numCount - 1].RowState);

for (int i = 0; i < toLeft; i++)
{
// 正确显示交叉行样式
rowState = (i % 2 != 0) ? DataControlRowState.Alternate : DataControlRowState.Normal;

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, rowState);
for (int j = 0; j < numCols; j++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
this.gvProgram.Controls[0].Controls.AddAt(numCount + 1 + i, row);
}
}
}

protected void gvProgram_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int newPageIndex = 0;
// 当点击first, last, previous and next按钮时处理
newPageIndex = e.NewPageIndex;
// 对PageIndex溢出处理
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= this.gvProgram.PageCount ? this.gvProgram.PageCount - 1 : newPageIndex;
// 更新PageIndex
this.gvProgram.PageIndex = newPageIndex;
}



...全文
303 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
萬里無雲 2008-05-13
  • 打赏
  • 举报
回复
解决了贴出来分享一下,使用绑定空白行到数据源的方法

BLLFile bllFile = new BLLFile();
//取得数据集
DALItoy.TblFileDataTable dataTable = bllFile.GetListByFileTypeAndHost(this.hidHost.Text, this.hidFileType.Text);
//取得记录总数
recordCount = dataTable.Rows.Count;

//补空行
for (int i = recordCount%gvProgram.PageSize; i < gvProgram.PageSize; i++)
{
DALItoy.TblFileRow row = dataTable.NewTblFileRow();
row.FileName = " ";
row.FilePath = " ";
row.FileSize = 0;
row.FileType = " ";
dataTable.AddTblFileRow(row);
}

//绑定数据
this.gvProgram.DataSource = dataTable;
this.gvProgram.DataBind();



而对于空白行中显示了按钮,可以用Visible='<%# Eval("FileName") != " "%>'来控制

<asp:ImageButton ID="downloadPM" runat="server" Visible='<%# Eval("FileName") != " "%>' OnClick="ImageButton_Click" CommandArgument='<%# Eval("FilePath") %>'/>



如果数据源里有不容许为空的字段怎么办呢?如果上面的FileSize。可以自己再定义一个数据集,把原来的数据集的数据拷贝进来,空行就可以全写空不用担心不能为空的情况了。


过2天结贴谢谢大家,如果有更好的办法希望一起分享!
silwol 2008-05-09
  • 打赏
  • 举报
回复
把异常帖下看看是什么,也许能帮LZ解决呢
萬里無雲 2008-05-09
  • 打赏
  • 举报
回复
加gvProgram.DataBind();出异常

空白行写的数据源里是可以,但是空白行的按钮和序号也显示了,怎么去掉阿?
liyin_liu 2008-05-09
  • 打赏
  • 举报
回复
DING
hm8030 2008-05-09
  • 打赏
  • 举报
回复
不過以空白添加應該寫在數據源中,這樣就不會有這樣的問題了。
silwol 2008-05-09
  • 打赏
  • 举报
回复
少了gvProgram.DataBind();
yagebu1983 2008-05-09
  • 打赏
  • 举报
回复
顶了!!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧