Gridview 中所有行都有一个删除按钮

发布于 2024-10-17 07:07:10 字数 2967 浏览 3 评论 0原文

我有一个像这样的简单页面。 (EKLE = ADD, SıL = DELETE)

在此处输入图像描述

我的 AVUKAT 桌子是这样的。

在此处输入图像描述

简化,当我选择第一个下拉列表 MUSTERI 和第二个下拉列表 AVUKAT 时,添加数据库(获取 HESAP (数字)自动)或删除数据库和gridview。

这是我的代码。

添加点击

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

删除点击

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

我的经理想要的,删除过程非常困难。让我们轻松一下。因为当我想删除一些数据时,我必须选择两个下拉列表,然后选择删除(Sil)按钮。

我们应该更喜欢每一行都有一个删除按钮。然后当我们点击按钮时。然后必须删除gridviewANDdatabse。

我在互联网上找到了关于我想要的完美例子。

在此处输入图像描述

我想如果我们能做到这一点,Sil(Delete) 按钮应该被删除。

我们怎样才能做到这一点?

我认为 AutoDeleteButton 不能像这样工作。正确的?它刚刚从 Gridview 中删除。不是数据库。但我想删除两者(Gridview AND 数据库)

I have a simple page like this. (EKLE = ADD, SİL = DELETE)

enter image description here

And my AVUKAT Table like this.

enter image description here

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.

enter image description here

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-10-24 07:07:10

您需要确保调用正确的方法。

http://msdn.microsoft.com/en -us/library/system.web.ui.webcontrols.gridview_events.aspx

单击该按钮将运行“RowDeleting”方法,然后运行“RowDeleted”。

编辑 - 忘记提及,一旦运行已删除的方法,您将需要重新绑定网格视图。

GridView1.DataBind()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文