使用GridView分页会在gridview行数据绑定事件上导致错误,行索引错误 [英] Using GridView Paging causes error on gridview row databound event, row index error
问题描述
大家好,
我有一个gridview,在那行数据绑定事件运行良好,但是当我使用gridview分页时,它显示错误"
Hi all,
I have a gridview, in that, row databound event is working fine, but when i am using gridview paging, its says the error "
index was out of range. must be non-negative and less than the size of the collection.
"
Gridview设计
"
Gridview design
<asp:GridView ID="grvRecords" runat ="server" AutoGenerateColumns="false" Font-Bold="True"
Font-Names="Trebuchet MS" Font-Size="10px" AllowPaging="True"
Font-Underline="False" onrowcommand="grvRecords_RowCommand"
onpageindexchanging="grvRecords_PageIndexChanging" AllowSorting="True"
onsorting="grvRecords_Sorting" PageSize="5" DataKeyNames="iPatientId,iCodingId" >
<columns>
<asp:TemplateField >
<HeaderTemplate >
<input id="chkAllItems" type="checkbox" önclick="CheckAllDataGridCheckBoxes('chkItemChecked',document.forms[0].chkAllItems.checked)" value="Check">
</HeaderTemplate>
<itemtemplate>
<asp:CheckBox id="chkBulk" runat="server" Width="30px" AutoPostBack="False">
</itemtemplate>
<asp:TemplateField Visible="false" >
<itemtemplate>
<asp:Label ID="lblCodingId" runat ="server" Text='<%#Eval("iCodingId") %>' >
</itemtemplate>
<asp:TemplateField HeaderText="AHSL#" SortExpression="iPatientId" >
<itemtemplate>
<asp:Label ID="lbliPatientId" runat ="server" Text='<%#Eval("iPatientId") %>' >
</itemtemplate>
<asp:BoundField DataField ="UploadDate" HeaderText="UploadDate" SortExpression="UploadDate" />
<%--<asp:ImageButton ID="imgCPTICD" runat ="server" ImageUrl="~/Images/Plus.JPG" CommandName="Show" CommandArgument='<%# Container.DataItemIndex%>' />--%>
<asp:ImageButton ID="imgCPTICD" runat ="server" ImageUrl="~/Images/Plus.JPG" CommandName="Show" CommandArgument='<%# DataBinder.Eval(Container,"DataItemIndex") %>' />
<table>
<tr>
<td colspan="6">
<asp:GridView ID="grvCPTICD" runat ="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3399FF" HeaderStyle-ForeColor="White">
</columns>
<HeaderStyle Font-Bold="True" Font-Underline="False" ForeColor="#0033CC"
BackColor="#6699FF" Font-Names="Trebuchet MS" Font-Size="12px" />
在row-DataBound事件中
In row-DataBound event
protected void grvRecords_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Show")
{
int RowIndex = Convert.ToInt32((e.CommandArgument).ToString());
<big>ImageButton imgBtn = (ImageButton)grvRecords.Rows[RowIndex].FindControl("imgCPTICD");</big> //Here i am getting the error while using paging, without paging it is coming correct.
if (imgBtn.ImageUrl == "~/Images/Plus.JPG")
{
Label lblAHSL = (Label)grvRecords.Rows[RowIndex].FindControl("lbliPatientId");
GridView grvSub = (GridView)grvRecords.Rows[RowIndex].FindControl("grvCPTICD");
BindInnerGridView(grvSub, Convert.ToInt32(lblAHSL.Text));
imgBtn.ImageUrl = "~/Images/Minus.JPG";
}
else if (imgBtn.ImageUrl == "~/Images/Minus.JPG")
{
GridView grvSub = (GridView)grvRecords.Rows[RowIndex].FindControl("grvCPTICD");
grvSub.DataSource = null;
grvSub.EmptyDataText = "";
grvSub.DataBind();
imgBtn.ImageUrl = "~/Images/Plus.JPG";
}
}
}
catch (Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = ex.Message;
}
}
这是我的pageIndex更改代码
This is my pageIndex change code
protected void grvRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvRecords.PageIndex = e.NewPageIndex;
BindGridview();
}
谁能发送解决方案plz
在此先感谢
Murthy
can any one send the solution plz
Thanks in advance
Murthy
推荐答案
尝试更新您的图片按钮html,如下所示
Try updating you image button html somelike below
<asp:ImageButton runat="server" ID="imgCPTICD"
ImageUrl="~/Images/Plus.JPG" CommandName="Show"
CommandArgument="<%#((GridViewRow)Container).RowIndex%>"/>
如果仅将索引用于查找图像"按钮,则可以替换下面的代码
您可以直接将发件人作为Show(+)按钮
当前代码
If you are using Index only for finding the Image button then you can replace the code like below
You can directly take the sender as the Show(+) button
Current Code
ImageButton imgBtn = (ImageButton)grvRecords.Rows[RowIndex].FindControl("imgCPTICD");
新密码
New Code
ImageButton imgBtn = (ImageButton)sender;
这篇关于使用GridView分页会在gridview行数据绑定事件上导致错误,行索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!