الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Delete / Update Gridview using Code

مغلق
بدأه eng_seham في 25 أبريل 2007 · 5 رد · 1,932 مشاهدة · في ADO.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

please I want Delete and update from gridview using code but not work

this is my code :

/////////////////////////////////// Delete ///////////////////////////////////

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

SqlConnection cn = new SqlConnection();

cn.ConnectionString = ConfigurationManager.ConnectionStrings["ACCITConnectionString"].ConnectionString;

SqlCommand cmd = new SqlCommand("Delete From IdentityType Where IdentityType_ID=" +e.RowIndex , cn);

cn.Open();

cmd.ExecuteNonQuery();

cn.Close();

GridView1.EditIndex = -1;

GridView1.DataBind();

}

/////////////////////////////////// update ///////////////////////////////

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

int index = GridView1.EditIndex;

GridViewRow row = GridView1.Rows[index];

SqlConnection cn = new SqlConnection();

cn.ConnectionString = ConfigurationManager.ConnectionStrings["ACCITConnectionString"].ConnectionString;

SqlCommand cmd = new SqlCommand("update IdentityType set Where IdentityType_ID=" + e, cn);

cn.Open();

cmd.ExecuteNonQuery();

cn.Close();

//Cancel Selected Record After Delete

GridView1.EditIndex = -1;

//Display Data in DataGride

GridView1.DataBind();

}

please help me where is the problem

#2

تسنخدم الاقول

لكتابة الاكواد بهذا الشكل وهي متخصصة بالتنسيق
  [  code]
[code]
#3

please I want Delete and update from gridview using code but not work

this is my code :

[protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{


SqlConnection cn = new SqlConnection();

cn.ConnectionString = ConfigurationManager.ConnectionStrings["ACCITConnectionString"].ConnectionString;

SqlCommand cmd = new SqlCommand("Delete From IdentityType Where IdentityType_ID=" +e.RowIndex , cn);

cn.Open();

cmd.ExecuteNonQuery();

cn.Close();

GridView1.EditIndex = -1;

GridView1.DataBind();

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

int index = GridView1.EditIndex;

GridViewRow row = GridView1.Rows[index];

SqlConnection cn = new SqlConnection();

cn.ConnectionString = ConfigurationManager.ConnectionStrings["ACCITConnectionString"].ConnectionString;

SqlCommand cmd = new SqlCommand("update IdentityType set Where IdentityType_ID=" + e, cn);

cn.Open();

cmd.ExecuteNonQuery();

cn.Close();

//Cancel Selected Record After Delete

GridView1.EditIndex = -1;

//Display Data in DataGride 

GridView1.DataBind();



}
#4

برافووووووووووووووووووووووووووووووووووو :)

#5

فين المشكلة بقا في الكود ؟؟؟؟

#6

السلام عليكم

لتفسخ أسطر من DataGridView بإمكانك إستعمال الكود التالي علما أن تلك البيانات ستفسخ أيضا من قاعدة البيانات:

private void btnDelete_Click(object sender, EventArgs e)
{// Warning message
	   if (MessageBox.Show("Do you really want to delete selected record(s)?", 
		   "Delete Warning", MessageBoxButtons.YesNo,
		   MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
		   == DialogResult.Yes)
		{
		  try
		   {// connection is SqlConnection
			   connection.Open();
			   // userDataGridView is DataGridView
			   int cnt = userDataGridView.SelectedRows.Count;
			   for (int i = 0; i < cnt; i++)
			   {
				   if (this.userDataGridView.SelectedRows.Count > 0 &&
					   this.userDataGridView.SelectedRows[0].Index !=
					   this.userDataGridView.Rows.Count - 1)
					{// Remove selected items
					   this.userDataGridView.Rows.RemoveAt(
					   this.userDataGridView.SelectedRows[0].Index);
					}
			   }
			   //adapter is SqlDataAdapter
			   //userTable is DataTable
			   adapter.Update(userTable);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			finally
			{ //close the connection
			  connection.Close();
						  }
	   }
}

لتحديث البيانات

private void btnUpdate_Click(object sender, EventArgs e)
{
   try
	{				
	  connection.Open();
	  adapter.Update(userTable);
	  userDataGridView.ReadOnly = true;
	}
   catch (Exception ex)
	{
	  MessageBox.Show(ex.ToString());
	}
   finally
	{
	  connection.Close();
	}
}

والله أعلم

هذا الموضوع مغلق.

مواضيع مشابهة