سلام
چجوری یک سطر از گرید ویو با دگمه ای که توسط خود گرید ویو بهش اضاف کردم رو حذف کنم؟
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
این وسط چی بنویسم؟؟؟؟؟؟؟؟
}
سلام
چجوری یک سطر از گرید ویو با دگمه ای که توسط خود گرید ویو بهش اضاف کردم رو حذف کنم؟
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
این وسط چی بنویسم؟؟؟؟؟؟؟؟
}
GridView1.DeleteRow(GridView1.SelectedIndex);
بعدش برای اعمال تغییرات در دیتابیست باید binding source رو آپدیت کنی.
موفق باشید
دوست عزیز ارور میده موقع اجرا.
Server Error in '/LINQWEBSITE' Application.
Deleting is not supported by data source 'SqlDataSource2' unless DeleteCommand is specified.
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.NotSupportedException: Deleting is not supported by data source 'SqlDataSource2' unless DeleteCommand is specified.
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.
Stack Trace:
[NotSupportedException: Deleting is not supported by data source 'SqlDataSource2' unless DeleteCommand is specified.]
System.Web.UI.WebControls.SqlDataSourceView.Execut eDelete(IDictionary keys, IDictionary oldValues) +256
System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +210
System.Web.UI.WebControls.GridView.HandleDelete(Gr idViewRow row, Int32 rowIndex) +1231
System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean causesValidation, String validationGroup) +1533
System.Web.UI.WebControls.GridView.RaisePostBackEv ent(String eventArgument) +245
System.Web.UI.WebControls.GridView.System.Web.UI.I PostBackEventHandler.RaisePostBackEvent(String eventArgument) +42
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +37
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +289
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5348
سلام به نظرم دلیلش اینه که شما با روش کد اطلاعات رو داخل گرید ریختین که در این صورت فکر نمی کنم رفرش کردن بایند جواب بده
بهتره با دستور اس کیو ال این کارو انجام بدین و در نهایت ارتباط رو از طریق کد رفرش کنید تا اطلاعات جدید دیده بشه
سلام
یک روتین بنویسید و توی توی رویداد Gridview1_CellContentClick باتنی که به دیتاگراید اضافه کردید بنویسید
private void DeleteDefainLesson()
{
try {
using (SqlConnection cnn = new SqlConnection(GetConnetctionString)) {
using (SqlCommand cmd1 = new SqlCommand("DELETE FROM DefineLessonDetails WHERE DefLessonRowID = @DefLessRow", cnn)) {
cmd1.Parameters.AddWithValue("@DefLessRow", Convert.ToInt32(MyGridview1.CurrentRow.Cells("ColR owID").Value));
cnn.Open();
if (MessageBox.Show("اگر مايل به حذف اين رديف هستيد دكمه تاييد را بزنيد", "حذف", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) {
cmd1.ExecuteNonQuery();
}
}
cnn.Close();
}
} catch (Exception ex) {
MessageBox.Show("پیام خطا...");
}
}
من از linq استفاده کردم.
من یه ستون یه گرید ویو اضاف کردم و یه باتن داخلش گذاشتم.
حالا میخوام وقتی روش کلیک میشه اون سطر از گرید ویو و دیتابیس حذف بشه!
فرض می کنیم شما با این کد یه ستون حاوی باتن deleteدرون گرید اضافه کردین
private System.Windows.Forms.DataGridViewButtonColumn Column1;
this.Column1.HeaderText = "delete";
this.Column1.Name = "Column1";
this.Column1.Text = "delete";
this.Column1.UseColumnTextForButtonValue = true;
حالا می خواهیم با فشار دادن دکمه هر سطر اونو حذف کنیم اینطوری :
در رویداد CellMouseClick گرید تون می نویسین
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//اندیس ستون مربوط به دکمه حذف
if (e.ColumnIndex == 4)
{
dataGridView1.Rows.Remove(dataGridView1.Rows[e.RowIndex]);
}
}