All Row has a Delete Button in Gridview

邮差的信 提交于 2019-12-11 05:21:03

问题


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);

        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)


回答1:


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()



来源:https://stackoverflow.com/questions/4955759/all-row-has-a-delete-button-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!