简体   繁体   中英

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

        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)

This link might be useful to you. Also have a look at this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM