[英]deleting a selected row from a DataGridView with MS Access DB
having problems again. 再次遇到问题。 I am able to delete the row from the DVG but when i close the program and re run it the row reappears. 我可以从DVG中删除该行,但是当我关闭程序并重新运行它时,该行会重新出现。 I have had a look at a few examples online and really struggling to get his to work. 我在网上看过一些示例,但确实很难使他工作。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class FormAccounts : Form
{
public FormAccounts()
{
InitializeComponent();
this.accountsTableAdapter.Fill(this.accountsDataSet.Accounts);
}
private void BTNADD_Click(object sender, EventArgs e)
{
MessageBox.Show("Make sure you have checked the Date before Adding");
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kenny\Documents\Visual Studio 2010\Projects\Copy Cegees\Cegees\Cegees\Accounts.accdb";
String TheDate = DTPAccounts.Value.ToShortDateString();
String Moneyin = TextMoneyin.Text;
String Retailin = TextRetailin.Text;
String Rent = TextRent.Text;
String Stock = TextStock.Text;
String Misc = TextMisc.Text;
String Water = TextWater.Text;
String Fuel = TextFuel.Text;
String Phone = TextPhone.Text;
OleDbCommand cmd = new OleDbCommand("Insert Into Accounts ([TheDate], [Moneyin], [Retailin], [Rent], [Stock], [Misc], [Water], [Fuel], [Phone]) Values (@TheDate, @Moneyin, @Retailin, @Rent, @Stock, @Misc, @Water, @Fuel, @Phone)");
cmd.Connection = conn;
conn.Open();
if (conn.State == ConnectionState.Open)
{
cmd.Parameters.Add("@TheDate", OleDbType.VarChar).Value = TheDate;
cmd.Parameters.Add("@Moneyin", OleDbType.VarChar).Value = Moneyin;
cmd.Parameters.Add("@Retailin", OleDbType.VarChar).Value = Retailin;
cmd.Parameters.Add("@Rent", OleDbType.VarChar).Value = Rent;
cmd.Parameters.Add("@Stock", OleDbType.VarChar).Value = Stock;
cmd.Parameters.Add("@Misc", OleDbType.VarChar).Value = Misc;
cmd.Parameters.Add("@Water", OleDbType.VarChar).Value = Water;
cmd.Parameters.Add("@Fuel", OleDbType.VarChar).Value = Fuel;
cmd.Parameters.Add("@Phone", OleDbType.VarChar).Value = Phone;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Customer Added");
conn.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Source);
MessageBox.Show(ex.ToString());
conn.Close();
}
}
}
private void BTNView_Click(object sender, EventArgs e)
{
DVGAccounts.Visible = !DVGAccounts.Visible;
}
private void BTNDelete_Click(object sender, EventArgs e)
{
{
DialogResult dr = MessageBox.Show("Are you sure you want to delete", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
if (this.DVGAccounts.SelectedRows.Count > 0)
{
DVGAccounts.Rows.RemoveAt(DVGAccounts.CurrentRow.Index);
this.accountsTableAdapter.Update(this.accountsDataSet.Accounts);
accountsDataSet.AcceptChanges();
;
}
}
}
}
}
}
I managed to get it working using this code on the button. 我设法使用按钮上的此代码使其工作。
private void BTNDelete_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to delete", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
if (this.DVGAccounts.SelectedRows.Count >0 && this.DVGAccounts.SelectedRows[0].Index != this.DVGAccounts.Rows.Count -1)
{
this.DVGAccounts.Rows.RemoveAt(this.DVGAccounts.SelectedRows[0].Index);
}
}
}
You're removing the row from the DataGridView only, which is an in-memory construct. 您仅从DataGridView中删除该行,它是一个内存构造。 If you want it to delete the row from the database, you must either have DataGridView which is configured to be connected to a table through an open connection, or react to removing records, by issuing a delete statement, in the same sort of way as you're doing with your inserts. 如果希望它从数据库中删除行,则必须具有配置为通过打开的连接连接到表的DataGridView,或者必须通过发出delete语句以与以下相同的方式对删除记录做出反应您正在使用插入内容。 I'd recommend trying to get things a bit more automatic however, so a connected DGV would probably be beneficial. 我建议尝试使事情自动化一些,因此连接的DGV可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.