PDA

View Full Version : حذف گروهی رکورد ها از gridview



ali_mnkt
چهارشنبه 17 خرداد 1391, 20:39 عصر
با سلام

من برای حذف گروهی رکورد هایی در gridview از روش زیر استفاده می کنیک
اول یک datasource در نظر می گیریم که از یک جدول پایگاه پر می شه مثلا فیلد های ID,name رو واکشی میکنه
بعد این datasource رو به یک grid نسبت می دم در grid هم یک templatefield برای کد رکوردی که می خوام حذف کنم در نظر می گیرم و داخلش یک lable می ذارم بعد data binding مربوط به اون lable رو id رکورد های بازیابی شده قرار میدم . حالا یک templatefieldn دیگه در نظر می گیرم و داخلش checkbox می ذارم . grid به صورت زیر می شه


<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:shopConnectionString %>"
SelectCommand="SELECT , [Pname], [pprice], [Pimage] FROM [product]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
DataKeyNames="id"
onpageindexchanged="GridView1_PageIndexChanged"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" PageSize="2" ShowFooter="True"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Pname" HeaderText="Pname"
SortExpression="Pname" />
<asp:BoundField DataField="pprice" HeaderText="pprice"
SortExpression="pprice" />
<asp:BoundField DataField="Pimage" HeaderText="Pimage"
SortExpression="Pimage" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chDelete" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" CommandName="delete" Text="Button" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />

</asp:GridView>

خوب در رویداد rowcommand مربوط به grid هم کد زیر رو می نویسم



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("chDelete");

if (cb.Checked == true)
{
Label lblid = (Label)GridView1.Rows[i].FindControl("lblid");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["shopConnectionString"].ToString());
SqlCommand comm = new SqlCommand("delete from product where id=" + lblid.Text + "",conn );
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}

}

}
GridView1.DataBind();
}

اما وقتی دکمه delete رو میزنم خطای زیر رو داره . خیلی هم جستجو کردم اما به نتیجه ای نرسیدم . کسی می تونه مشکل رو حل کنه ؟

[I]Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

ali_mnkt
پنج شنبه 18 خرداد 1391, 12:36 عصر
از دوستان کسی نمی تونه کمک کنه ؟

naser_feb8646
پنج شنبه 18 خرداد 1391, 15:08 عصر
سلام دوست عزیز
به نظرم اگر for رو به شکل زیر تصحیح کنید درست بشه

for (int i = 0; i < GridView1.Rows.Count - 1; i++)

karim orooji
پنج شنبه 18 خرداد 1391, 16:17 عصر
http://barnamenevis.org/showthread.php?287957-%D8%A2%D8%B1%D8%B4%DB%8C%D9%88-%D8%B3%D9%88%D8%B1%D8%B3-%D9%87%D8%A7%DB%8C-%DA%A9%D8%A7%D8%B1%D8%A8%D8%B1%D8%AF%DB%8C-%D9%88-%D8%A8%D8%AF%D8%B1%D8%AF%D8%A8%D8%AE%D9%88%D8%B1&p=1270117&viewfull=1#post1270117

fakhravari
جمعه 19 خرداد 1391, 20:32 عصر
http://barnamenevis.org/showthread.php?319991-%D8%AB%D8%A8%D8%AA-%D8%AA%D9%85%D8%A7%D9%85%DB%8C-%D9%86%D9%85%D8%B1%D8%A7%D8%AA-%D8%A8%D8%A7-%DB%8C%DA%A9-%DA%A9%D9%84%DB%8C%DA%A9&highlight=%D8%AB%D8%A8%D8%AA+%D9%86%D9%85%D8%B1%D8 %A7%D8%AA+%D8%A8%D8%A7