All Row has a Delete Button in Gridview
I have a simple page like this. (EKLE = ADD
, SİL = DELETE
)
And my AVUKAT Table like this.
Simplify, When i choose first dropdown MUSTERI and second dropdown AVUKAT , add the database (getting HESAP (number) automaticly) or delete the database and gridview.
This is my code.
ADD Click
protected void Add_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
/*GridView1.DataSource = dr;
GridView1.Visible = true;*/
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
DELETE Click
protected void Delete_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("DELETE FROM AVUKAT WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP", myConnection);
开发者_如何学JAVA cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
/* GridView1.DataSource = dr;
GridView1.Visible = true;*/
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
My manager wants, delete process is so hard. Let's make easy. Because when i want to delete some data, i must choose two dropdownlist and then delete(Sil) button.
We should prefer that every row has a delete button. Then when we click the button. Then must be deleted gridview AND
databse.
I found perfect example on the internet abuot what i want.
I think if we can do that, Sil(Delete) Button should be deleted.
How can we do that?
I think AutoDeleteButton
don't work like this. Right? It just deleted from Gridview. Not Database. But i want deleting both (Gridview AND
Database)
You need to ensure you calling the correct method.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events.aspx
Clicking the button will run the "RowDeleting" method then "RowDeleted".
Edit - forgot to mention, once you've run your deleted method you'll need to rebind the gridview.
GridView1.DataBind()
精彩评论