PDA

View Full Version : سوال: مشکل عجیب در پاک کردن سطر های دیتاگرید ویو



modern_payam
دوشنبه 27 دی 1389, 13:25 عصر
با سلام و احترام

من یک دیتاگرید ویو دارم که سلولی دارد که از نوع چک باکس است می خواهم وقتی این چک باکس تیک خورد سطر مورد نظر پاک شود.
به جای اینکه با تیک خوردن چک باکس مقدار true بفرستد مقدار null می فرستد ولی وقتی 2 سطر را با هم انتخاب می کنیم در حالی که هر دو سطر چک باکس های آن تیک خورده فقط سطر دوم را پاک می کند. من چندین بار این کد را بدون خطا نوشته ام اما نمی دانم چرا اینطوری پاک می کند.
اگر 3 سطر انتخاب کنی 2 سطر آخر را پاک می کند.
اگر 4 سطر انتخاب کنی 3 سطر آخر را پاک می کند.
در هر صورت مقدارچک باکس سطر اول انتخابی را null می فرستد در حالی که من آن را تیک زده ام و باید true کند.

راههای رفته:
1- عوض کردن دیتاگرید ویو
2- کپی کردن کد از برنامه ای که درست اجرا می شد.
3-و........

ولی هیچ کدام از راه های رفته جواب نداد.
ممنون می شوم بگویید مشکل کار در کجاست ؟:متفکر:

MortezaGity
دوشنبه 27 دی 1389, 15:33 عصر
دوست عزیز از این کد استفاده کن

متد خذف سطر

#region Multiple Delete
private void DeleteRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
StringBuilder sb = new StringBuilder(string.Empty);

foreach (string item in sc)
{
const string sqlStatement = "DELETE FROM Customers WHERE CustomerID";
sb.AppendFormat("{0}='{1}'; ", sqlStatement, item);
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
#endregion


کد مربوط به باتنی که کار حذف رو انجام میده


protected void ButtonDelete_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;

for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
if (cb != null)
{
if (cb.Checked)
{
id = GridView1.Rows[i].Cells[1].Text; // get the id of the field to be deleted
sc.Add(id); // add the id to be deleted in the StringCollection
}
}
}

DeleteRecords(sc); // call method for delete and pass the StringCollection values
BindGridView(); // Bind GridView to reflect changes made here
}


کد رویداد RowDataBound مربوط به گرید ویو

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) //check for RowType
{
Button b = (Button)e.Row.FindControl("ButtonDelete"); //access the LinkButton from the Header TemplateField using FindControl
b.Attributes.Add("onclick", "return ConfirmOnDelete();"); //attach the JavaScript function
}
}