حذف سطرهاي انتخاب شده در gridview
سلام دوستان
من بايد يک پروژه کتابخانه الکترونيکي تا چند وقت ديگه تحويل بدم ولي يک مشکلي دارم :
من در قسمت ادمين سايت مي خوام اين امکان بزارم که ادمين اسم يک کاربر سرچ بزنه بعد از اينکه پيدا کرد تو يک گرايد ويو نشون بده بعد ازاينکه نشون داد مدير بتونه سطرهايي که مي خواد انتخاب کنه (تا اينجا مشکلي ندارم.)و با استفاده از يک کليد همه سطرهاي انتخاب شده را پاک کنه . مشکل من با اين حذف کردن . نمي دونم چطوري بهش بفهمونم . از اينترنت هم خيلي سرچ کردم که نتيجش کد زير است که البته اون هم ارور زيرو مي ده . مخم داره سوت مي کشه . تو رو خدا يکي بگه اشکال کار من کجاست.
تو رو خدا نخنديد . من تازه کارم.
کد:
Erorr:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
protected void btnfinduser_Click(object sender, EventArgs e)
{
if (txtname.Text != "")
{
SqlConnection Con = new SqlConnection(SqlDataSource1.ConnectionString);
string SqlStr = "SELECT [UserId],[UserName],[AcountType],[FirstName],[LastName], [Email], [Gender], [BirthYear], [BirthMonth], [BirthDay], [SignUpDate] FROM [UserAcount] WHERE UserName Like N'%'+@UserName+'%'";
SqlCommand Cmd = new SqlCommand(SqlStr, Con);
Cmd.Parameters.AddWithValue("@UserName", txtname.Text);
Con.Open();
int I = Cmd.ExecuteNonQuery();
if (i != 0)
{
Grv_Search.DataSource = Cmd.ExecuteReader();
Grv_Search.DataBind();
Con.Close();
}
else
{
Label1.Text = "not find user";
}
}
else
{
Label1.Text = "لطفا نام کاربري مورد نظر خود را وارد کنيد";
}
}
protected void btndeleteuser_Click(object sender, EventArgs e)
{
for (int j = 0; j < Grv_Search.Rows.Count; j++)
{
GridViewRow row = Grv_Search.Rows[j];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (isChecked)
{
int Username = Convert.ToInt32(Grv_Search.DataKeys[row.RowIndex].Value);
// Pass the value of the selected Employye ID to the Delete //command.
SqlDataSource1.DeleteParameters["Username"].DefaultValue = Username.ToString();
SqlDataSource1.Delete();
}
}
}
ممنون