سلام به تمامی کاربران محترم سایت
یه مشکل کوچولو دارم ببینم کسی میتونه کمک کنه
من یه دونه گرید ویو دارم که یکی از سطونهاش چک باکس هست
میخواهم کاربر هر سطی رو که تیک زد حذف کنه
این کد من هست
CheckBox chkRemove=newCheckBox();
DataBoundLiteralControl DBLC;
string strIDs= "";
bool boolCheck= false;
foreach (GridViewRow gvRow in GridView_NewsList.Rows)
{
chkRemove = (CheckBox)GridView_NewsList.FindControl("ChkRemove");
if (chkRemove.Checked == true)
{
boolCheck = true;
DBLC = (DataBoundLiteralControl)GridView_NewsList.FindControl("ItemNumRemove").Controls[0];
strIDs += DBLC.Text + ",";
}
}
if (boolCheck == true)
{
string strSQL = "Delete * From news Where Id IN ('" + strIDs.Substring(0, strIDs.LastIndexOf(",")) + "')";
SqlConnection con = newSqlConnection(st);
SqlCommand cmd = newSqlCommand(strSQL,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (SqlException ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
}
}
اینم کد گرید ویو
<asp:GridViewID="GridView_NewsList"runat="server"AutoGenerateColumns="False"DataKeyNames="Id"
DataSourceID="SqlDataSource1"Font-Names="Tahoma"Font-Size="9pt"Font-Strikeout="False"
Width="732px"OnSelectedIndexChanged="GridView_NewsList_SelectedIndexChanged"OnRowDataBound="GridView_NewsList_RowDataBound"EmptyDataText="چیزی برای نمایش وجود ندارد"AllowPaging="True"OnRowDeleting="GridView_NewsList_RowDeleting"ShowFooter="True">
<FooterStyleBackColor="Wheat"/>
<EmptyDataRowStyleBackColor="SteelBlue"Wrap="True"/>
<Columns>
<asp:TemplateField>
<FooterTemplate>
<asp:ButtonID="btnRemove"runat="server"BackColor="#FFC080"BorderStyle="Solid"
BorderWidth="1px"CommandName="delete"Font-Names="Tahoma"Font-Size="11px"Height="22px"
Text="حذف"Width="64px"/>
</FooterTemplate>
<ItemTemplate>
<asp:CheckBoxID="ChkRemove"runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandFieldButtonType="Button"DeleteText="حذف خبر"SelectText="ویرایش"ShowSelectButton="True"UpdateText="save">
<ItemStyleBorderWidth="1px"/>
<ControlStyleBackColor="Gainsboro"BorderColor="#404040"BorderWidth="1px"/>
</asp:CommandField>
<asp:BoundFieldDataField="Username"HeaderText="نویسنده"SortExpression="Username"/>
<asp:BoundFieldDataField="Time"HeaderText="ساعت درج"SortExpression="Time">
<ItemStyleWidth="100px"/>
</asp:BoundField>
<asp:BoundFieldDataField="Date"HeaderText="تاریخ"SortExpression="Date"/>
<asp:TemplateFieldFooterText="خلاصه خبر"HeaderText="خلاصه خبر"SortExpression="Body">
<ItemStyleWidth="150px"/>
<ItemTemplate>
<asp:PanelID="Panel2"runat="server"Height="50px"ScrollBars="Vertical"Width="221px">
<asp:LabelID="Label1"runat="server"Text='<%# Eval("Body") %>'></asp:Label></asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldFooterText="عنوان"HeaderText="عنوان">
<ItemStyleWidth="130px"/>
<ItemTemplate>
<asp:LabelID="Label5"runat="server"Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
<AlternatingItemTemplate>
</AlternatingItemTemplate>
</asp:TemplateField>
<asp:BoundFieldDataField="Id"HeaderText="کد خبر"InsertVisible="False"ReadOnly="True"
SortExpression="Id">
<ItemStyleWidth="35px"/>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LabelID="ItemNumRemove"runat="server"Visible="False"Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyleBackColor="InactiveCaptionText"/>
<EditRowStyleBackColor="CornflowerBlue"/>
<SelectedRowStyleBackColor="PeachPuff"/>
<HeaderStyleBackColor="Wheat"Font-Names="Arial"Font-Size="Medium"ForeColor="#400000"/>
</asp:GridView>
وقتی اجرا میکنم این ارور رو میده
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index