【问题标题】:Delete data row from DB in DataGridView winforms?从 DataGridView winforms 中的数据库中删除数据行?
【发布时间】:2013-03-30 21:22:25
【问题描述】:

我想从数据库中删除该行数据,目前我可以通过点击删除按钮删除该行数据,但是数据库表没有更新,我该怎么做?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class frmTestPrint : Form
    {
        public frmTestPrint()
        {
            InitializeComponent();
        }

        private void frmTestPrint_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
            this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);

        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
        }
    }
}

【问题讨论】:

  • 如果您也尝试从数据库中删除该行,您不认为同时发布您的 SQL 命令代码是个好主意吗?事实上,您只发布了影响datagridview 的代码。
  • 我没有 SQL 命令...我使用了 datagridview 并通过属性选择了数据源...
  • 是这个问题吗?这就是我不能删除和更新 SQL 数据库表的原因吗?
  • 不一定,不。这只是一个建议的起点。您需要做的不仅是更新您的datagridview,还要更新数据库。请参阅下面的答案。
  • 谢谢。下面的答案有些令人困惑,这个问题有很多答案......哪一个最能指导我?

标签: c# winforms datagridview sql-update


【解决方案1】:
private void Delete()
    {

        int i = 0;
        con1 = getConnection();
        con1.Open();
        SqlCommand storedProcedureCommand = con1.CreateCommand();
        storedProcedureCommand.CommandType = CommandType.Text;
        storedProcedureCommand.CommandText = "DELETE FROM name of your table WHERE pkeyID = @pkeyID";

        SqlParameter inparam2 = new SqlParameter("@pkeyID", SqlDbType.Int);
        inparam2.Direction = ParameterDirection.Input;
        inparam2.Value = Convert.ToInt32(dataGridView2.Rows[i].Cells[0].Value);
        storedProcedureCommand.Parameters.Add(inparam2);
        storedProcedureCommand.ExecuteNonQuery();

    }    

【讨论】:

  • 当然要改变你的datagrid的名字,加上在witch cell中找到你的pkeyID,然后调用loaddata()方法就可以了!
  • 看起来不错,我目前正在处理其他事情,但我会再看看这个,如果它有效,会将您的回复标记为正确答案:) 干杯
【解决方案2】:

这只是一次删除一个。我仍在处理多个删除操作。

        if (MessageBox.Show("Sure you wanna delete?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
        {
            //get the index from the dataGridView
            int rowIndex = table1DataGridView.CurrentCell.RowIndex;
            //Remove from both the actual database & datagridview
            table1BindingSource.RemoveAt(rowIndex);
            //update table 1
            this.table1TableAdapter.Update(this.maquinasDataSet.Table1);
            //load table 1
            this.table1TableAdapter.Fill(this.maquinasDataSet.Table1);
        }

【讨论】:

    【解决方案3】:

    您实际上根本没有影响数据库。也许这会有所帮助:

    How to delete a selected DataGridViewRow and update a connected database table?

    您是否探索过使用 TableAdapter 获得的 .Update 方法?我认为这应该涵盖你所追求的。 http://msdn.microsoft.com/en-us/library/bz9tthwx(v=vs.100).aspx

    这行得通吗:

    private void btnDelete_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
            this.bookingTableAdapter.Update(this.usersDataSet1.Booking);
        }
    

    如果这不起作用,您将不得不返回一条错误消息,或者希望知道他们在做什么的人可以保释我...

    【讨论】:

    • 我查看了链接,似乎找不到真正有用的解决方案?非代码会从数据库中删除我的数据
    • 我认为最后一个应该让你接近......或者看看这里:msdn.microsoft.com/en-us/library/xzb1zw3x.aspx
    • 它不起作用,或者我应该说没有足够的指导,您知道您是否可以更好地解释这些步骤?谢谢
    • @bandaa 在我的回答中添加了更多内容。希望这会让你到达那里(如果它确实是正确的答案)。
    • 我尝试过类似的事情......但我已经尝试过你的答案,问题是,它不会从表中删除数据,它仍然只是从 datagridview UI 中删除数据但是不是来自数据库。
    【解决方案4】:

    Delete a value from the data grid using DataGridviewBUttonColumn

    private void delete_record()
        {
    
             if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
            {
                try
                {
                    //am taking connection string using a class called "sqlconnection_imventory()"
                    SqlConnection conn = Connection.sqlconnection_imventory();
                    //Cell[0] is my button column & Cell[1] is SID coumn
                    string a = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    string sql = "DELETE FROM grades WHERE SID='" + a + "'";
                    conn.Open();
                    SqlCommand delcmd = new SqlCommand(sql, conn);
                    delcmd.Connection = conn;
                    delcmd.ExecuteNonQuery();
                    conn.Close();
                    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                    MessageBox.Show("deleted");
                }
                catch (Exception)
                {
    
                    throw;
                } 
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多