且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从数据库中的DataGridView的WinForms删除数据行?

更新时间:2023-10-15 09:15:52

 私人无效删除(),
{

INT I = 0;
CON1 =的getConnection();
con1.Open();
的SqlCommand storedProcedureCommand = con1.CreateCommand();
storedProcedureCommand.CommandType = CommandType.Text;
storedProcedureCommand.CommandText =DELETE FROM你的表的WHERE pkeyID = @pkeyID名;

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

}


i want to delete the row of data from the database, currently i can delete the row of data by clicking on the delete button, but the database Table is not updated, how do i do so?

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

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

    }