raika17metal
یک شنبه 24 بهمن 1389, 16:38 عصر
من یه جدول دارم که توش کد گروه و نام گروه رو ذخیره می کنم.
برای حذف یک گروه، به GridView یک دکمه با نام btnDelete اضافه کردم. اما زمانی که میخوام عملیات حذف رو انجام بدم بعضی وقتا انجام میشه ولی بعضی وقتا با این خطا مواجه میشه :
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
کدهای ASP.NET
<div id="Groups">
<asp:GridView ID="gridViewGroups" runat="server" AutoGenerateColumns="False" DataKeyNames="fldTitleID"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" ForeColor="Black" GridLines="Vertical"
onrowcommand="gridViewGroups_RowCommand"
onrowdeleting="gridViewGroups_RowDeleting">
<Columns>
<asp:BoundField HeaderText="گروه" DataField="fldTitle"/>
<asp:ButtonField ButtonType="Image" CommandName="btnDelete" Text="حذف"
ImageUrl="~/Images/icon_delete.jpg" />
<asp:ButtonField ButtonType="Image" CommandName="btnEdit"
ImageUrl="~/Images/edit Icon.jpg" Text="ویرایش" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
کدهای C# :
protected void Page_Load(object sender, EventArgs e)
{
RefreshGridview();
}
protected void gridViewGroups_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnDelete")
{
//get id of selected row
int id = (int) gridViewGroups.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
if (Logic.Business.DeleteGroup(id))
{
RefreshGridview();
}
}
}
private void RefreshGridview()
{
DataTable dt = new DataTable();
dt = Logic.Business.GetGroups();
gridViewGroups.DataSource = dt;
gridViewGroups.DataBind();
}
لطفا کمک کنید.
برای حذف یک گروه، به GridView یک دکمه با نام btnDelete اضافه کردم. اما زمانی که میخوام عملیات حذف رو انجام بدم بعضی وقتا انجام میشه ولی بعضی وقتا با این خطا مواجه میشه :
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
کدهای ASP.NET
<div id="Groups">
<asp:GridView ID="gridViewGroups" runat="server" AutoGenerateColumns="False" DataKeyNames="fldTitleID"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" ForeColor="Black" GridLines="Vertical"
onrowcommand="gridViewGroups_RowCommand"
onrowdeleting="gridViewGroups_RowDeleting">
<Columns>
<asp:BoundField HeaderText="گروه" DataField="fldTitle"/>
<asp:ButtonField ButtonType="Image" CommandName="btnDelete" Text="حذف"
ImageUrl="~/Images/icon_delete.jpg" />
<asp:ButtonField ButtonType="Image" CommandName="btnEdit"
ImageUrl="~/Images/edit Icon.jpg" Text="ویرایش" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
کدهای C# :
protected void Page_Load(object sender, EventArgs e)
{
RefreshGridview();
}
protected void gridViewGroups_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnDelete")
{
//get id of selected row
int id = (int) gridViewGroups.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
if (Logic.Business.DeleteGroup(id))
{
RefreshGridview();
}
}
}
private void RefreshGridview()
{
DataTable dt = new DataTable();
dt = Logic.Business.GetGroups();
gridViewGroups.DataSource = dt;
gridViewGroups.DataBind();
}
لطفا کمک کنید.