2013-10-14 99 views
0

再次出現問題。我可以從DVG中刪除行,但是當我關閉程序並重新運行時,行會再次出現。我在網上看了幾個例子,並且真的很難讓他的工作。從MS Access數據庫的DataGridView刪除選定的行

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


      } 
     } 
    } 
} 

回答

0

您只從DataGridView中刪除行,這是一個內存中的構造。如果您希望它從數據庫中刪除行,則必須使DataGridView通過打開的連接被配置爲連接到表,或者通過發出刪除語句來做出反應以刪除記錄,方式與你正在做你的插入。我建議儘量讓事情自動化一點,所以連接的DGV可能會有所幫助。

1

我設法使用這個代碼在按鈕上工作。

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); 
     } 
    } 
} 
相關問題