[英]Delete data row from DB in DataGridView winforms?
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();
}
This is only to delete one at a time. 这只是一次删除一个。 I am still working on multiple deletes.
我仍然在进行多次删除。
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);
}
You're not actually impacting the database at all. 实际上你根本没有影响数据库。 Perhaps this will help:
也许这会有所帮助:
How to delete a selected DataGridViewRow and update a connected database table? 如何删除选定的DataGridViewRow并更新连接的数据库表?
Have you explored the .Update method you get with the TableAdapter? 您是否研究过使用TableAdapter获得的.Update方法? I think that should cover what you're after.
我认为这应该涵盖你所追求的。 http://msdn.microsoft.com/en-us/library/bz9tthwx(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/bz9tthwx(v=vs.100).aspx
Does this work: 这有用吗:
private void btnDelete_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
this.bookingTableAdapter.Update(this.usersDataSet1.Booking);
}
if that doesn't work, you're going to have to come back w/ an error message or hopefully someone who knows what they're doing can come bail me out... 如果这不起作用,你将不得不带着错误信息回来,或者希望知道他们正在做什么的人可以保释我...
Delete a value from the data grid using DataGridviewBUttonColumn 使用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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.