从数据库中的DataGridView的WinForms删除数据行? [英] Delete data row from DB in DataGridView winforms?

查看:124
本文介绍了从数据库中的DataGridView的WinForms删除数据行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中删除的数据行,现在我可以通过点击删除按钮删除的数据行,但不更新数据库表,我该怎么办呢?



 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;使用System.Windows.Forms的
;

命名项目
{
公共部分类frmTestPrint:表
{
公共frmTestPrint()
{
的InitializeComponent() ;
}

私人无效frmTestPrint_Load(对象发件人,EventArgs五)
{
// TODO:这行代码加载数据到usersDataSet1.Booking'表。您可以移动,或删除它,根据需要。
this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);

}


私人无效btnDelete_Click(对象发件人,EventArgs五)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow。指数);
}
}
}


解决方案

 私人无效删除(),
{

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

    }    

这篇关于从数据库中的DataGridView的WinForms删除数据行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆