私は 2 つの gridview を持っています。1 つは parent で、もう 1 つは child です。各 gridview には独自の OnRowCommand イベントがあります。gridview rowcommand が子 row に対して発生すると、親の rowcommand イベントが発生します。これにより、gvProject行コマンドエラーで次のエラーが発生します(プロジェクトが少なく、フォルダーが多い場合)
エラー: インデックスが範囲外です。負ではなく、コレクションのサイズより小さくなければなりません。パラメータ名:インデックス
どうすればこれを解決できますか? これは私の gridView です:
<asp:GridView ID="gvProject" runat="server" SkinID="ExpandableGrid" DataSourceID="odsProject" DataKeyNames="ID" OnRowDataBound="gvProject_RowDataBound" OnRowCommand="gvProject_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<i class="btnCollapseExpand icon contrast-color collapse icon-ellipsis-h fright" onclick="ExpandCollapseGridView(this);" style="cursor: pointer"></i>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Select %>">
<ItemTemplate>
<asp:CheckBox ID="chbSelect" runat="server" TabIndex="9" CssClass="contrast-checkbox" />
</ItemTemplate>
<ItemStyle CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:BoundField HeaderText="<%$ Resources:Titles,Code %>" DataField="Code" SortExpression="Code"></asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Titles,Name %>" DataField="Name" SortExpression="Name">
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-4" />
</asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Titles, Customer %>" DataField="Customer" SortExpression="Customer.Name">
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-4" />
</asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Titles,ProgressStatus %>" DataField="ProjectStatus" SortExpression="ProjectStatus.Name">
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-2" />
</asp:BoundField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, ActivityStatus%>" SortExpression="Status">
<ItemTemplate>
<%#System.Convert.ToString(Convert.ToBoolean(Eval("Status") ) ? Resources.Titles.Enable : Resources.Titles.Disable) %>
</ItemTemplate>
<ItemStyle CssClass="col-xs-3 col-sm-3 col-md-3 col-lg-2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Edit %>">
<ItemTemplate>
<a id="IbtnEdit" tabindex="11" href="ProjectDetail.aspx?Edit=<%# Eval("ID").ToString() %>" class="btnIcon hasTooltip" title="<%= Resources.Titles.EditProject %>"><i class="icon icon-edit"></i></a>
</ItemTemplate>
<ItemStyle CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Delete %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteProject" TabIndex="12" runat="server" CommandName="DeleteProject" title="<%$ Resources:Titles,DeleteProject %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icon-trash-o"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, AddFolder %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnAddFolder" TabIndex="13" runat="server" CommandName="AddFolder" title="<%$ Resources:Titles,AddFolder %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icomoon-icon-AddFolder"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="5%" CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr class="collapsible_panel" height="50px" id="detailRow<%# ((GridViewRow)Container).RowIndex %>">
<td colspan="100%">
<div class="collapsible_panel_content">
<asp:GridView ID="gvFolders" runat="server" DataKeyNames="ID" OnRowCommand="gvFolders_RowCommand">
<Columns>
<asp:BoundField HeaderText="<%$ Resources:Titles,FolderName %>" DataField="Name" SortExpression="Name">
<ItemStyle CssClass="col-xs-2 col-sm-2 col-md-2 col-lg-2" />
</asp:BoundField>
<asp:TemplateField HeaderText="<%$ Resources:Titles,FolderCode %>">
<ItemTemplate>
<%#System.Convert.ToString(Eval("Code").ToString().Trim())%>
</ItemTemplate>
<ItemStyle CssClass="col-xs-2 col-sm-2 col-md-2 col-lg-2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles,Creator %>">
<ItemTemplate>
<%#System.Convert.ToString(GetUser(Eval("OwnerID")))%>
</ItemTemplate>
<ItemStyle CssClass="col-xs-2 col-sm-2 col-md-2 col-lg-2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Edit %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnEditFolder" TabIndex="7" runat="server" CommandName="EditFolder" title="<%$ Resources:Titles,EditFolder %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icon-edit"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="center col-xs-1 col-sm-1 col-md-1 col-lg-1" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Titles, Delete %>">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteFolder" TabIndex="7" runat="server" CommandName="DeleteFolder" title="<%$ Resources:Titles,DeleteFolder %>" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CssClass="btnIcon hasTooltip">
<i class="icon icon-trash-o"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="center col-xs-1 col-sm-1 col-md-1 col-lg-1" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
<HeaderStyle CssClass="noPadding" />
<ItemStyle Width="0px" CssClass="noPadding" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<%= Resources.Messages.NoProjectIsForView %>
</EmptyDataTemplate>
</asp:GridView>
これは私のrowcommandイベントです:
protected void gvProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex, id;
rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView)sender;
if (grid.ID == "gvProject")
{
id = Convert.ToInt32(grid.DataKeys[rowIndex].Value);
switch (e.CommandName)
{
case "DeleteProject":
ClearForm();
hfID.Value = id.ToString();
HiddenField1.Value = id.ToString();
lblDeleteMessage.Text = string.Empty;
Global.ShowModalBox(upProjectDelete, "delete_modal");
break;
case "AddFolder":
ClearForm();
hfID.Value = id.ToString();
Global.ShowModalBox(upAddFolder, "AddFolderModal");
break;
}
}
}
protected void gvFolders_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex, id;
rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView)sender;
id = Convert.ToInt32(grid.DataKeys[rowIndex].Value);
switch (e.CommandName)
{
case "EditFolder":
if (grid.ID == "gvFolders")
{
ClearForm();
hfID.Value = id.ToString();
Folder folder = Providers.Folder.GetById(id);
txtFolderName.Text = folder.Name;
EditType = eEditType.Edit;
lblBoxTitle.Text = Resources.Titles.EditFolder;
Global.ShowModalBox(upAddFolder, "AddFolderModal");
}
break;
case "DeleteFolder":
if (grid.ID == "gvFolders")
{
ClearForm();
hfID.Value = id.ToString();
lblDeleteFolderMessage.Text = string.Empty;
Global.ShowModalBox(upAddFolder, "DeleteFolderModal");
}
break;
}
}