2013-09-23 67 views
0

我正在拍一個wpf申請。主窗口的樣子:http://www54.zippyshare.com/v/91622733/file.html如何在主窗口中顯示數據庫中新插入的記錄?

main window

主窗口代碼:

namespace WpfApplication25 
{ 
    /// <summary> <br> 
    /// Interaction logic for MainWindow.xaml <br> 
    /// </summary> <br> 
    public partial class MainWindow : Window <br> 
    { 

     int count = 120; 
     System.Windows.Threading.DispatcherTimer tmr = new System.Windows.Threading.DispatcherTimer(); 

     public MainWindow() 
     { 
      InitializeComponent(); 

      tmr.Interval = new TimeSpan(0, 0, 1); 
      tmr.Tick += new EventHandler(tmr_Tick); 

      DataTable aukcijeTable = new DataTable(); 
      SqlConnection conn = new SqlConnection(@"data source=(local);database=Aukcija;integrated security=true;"); 
      SqlDataAdapter aukcDa = new SqlDataAdapter("select * from auctions", conn); 

      aukcDa.Fill(aukcijeTable); 
      aukcija_bazeDataGrid.DataContext = aukcijeTable; 


     } 



     void tmr_Tick(object sender, EventArgs e) 
     { 
      label1.Content = count -= 1; 
      if (count == 0) 
      { 
       System.Windows.Forms.MessageBox.Show("Auction completed"); 
       tmr.Stop(); 
       count = 120; 
      } 
      else 
      { 


      } 

     } 


     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      Form1 popup = new Form1(); 
      popup.ShowDialog(); 
      popup.Dispose(); 
     } 

     private void button3_Click(object sender, RoutedEventArgs e) 
     { 
      Form2 popup = new Form2(); 
      popup.ShowDialog(); 

      popup.Dispose(); 
     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      tmr.Start(); 

      using (SqlConnection conn = new SqlConnection(@"data source=(local);database=Aukcija;integrated security=true;")) 

      { 
       DataTable cena1 = new DataTable(); 
       conn.Open(); 
       SqlDataAdapter DA = new SqlDataAdapter(" UPDATE auctions SET current_price = current_price + 1", conn); 
       SqlCommand cmd = new SqlCommand ("UPDATE auctions SET current_price = current_price + 1", conn); 
       DA.Fill(cena1); 
       //DA.Update(cena1); 
       cmd.ExecuteNonQuery(); 
       SqlCommandBuilder cb = new SqlCommandBuilder(DA); //novo 
       DA.Update(cena1); //novo 
       conn.Close(); 

      } 

     } 


     private void aukcija_bazeDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 

     } 

     private void button4_Click(object sender, RoutedEventArgs e) 
     { 

      tmr.Start(); 

     } 

     private void button5_Click(object sender, RoutedEventArgs e) 
     { 
      tmr.Stop(); 
      System.Windows.Forms.MessageBox.Show("Auction completed!"); 
      count = 120; 
     } 


    } 
} 

而且,我做了一個新的形式增加新的拍賣,它看起來像這樣: http://www8.zippyshare.com/v/35519167/file.html

new auction

代碼爲新的形式:

namespace WpfApplication25 { 
public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     using (SqlConnection connection = new SqlConnection(
        @"data source=(local); 
        database=Aukcija; 
        integrated security=true;")) 
     { 
      DataTable aukcijeTable = new DataTable(); //novo 

      SqlCommand cmd = new SqlCommand("INSERT INTO Auctions (item_name, start_price, current_price) VALUES (@item_name, @start_price, @current_price)"); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = connection; 
      connection.Open(); 
      cmd.Parameters.AddWithValue("@item_name", textBox1.Text); 
      cmd.Parameters.AddWithValue("@start_price", textBox2.Text); 
      cmd.Parameters.AddWithValue("@current_price", textBox3.Text); 
      cmd.ExecuteNonQuery();   
      connection.Close(); 



     } 
    } 
}} 

當我點擊button1_Click它打開了我一個新的形式和我有新的拍賣信息填充它,當我點擊確定沒有任何反應。我必須關閉我的應用程序並再次打開它,以顯示我從DataBase中新插入的記錄。

我在代碼中缺少什麼? 我需要點東西刷新(更新)主窗口,當我按下確定...

回答

2

在你MainForm(你打開更新-F ORM)代替,你目前正在使用下面的代碼打開更新的形式部分:

if (popup.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
{ 
    //to update your DataGrid, try the following:    
    SqlConnection conn = new SqlConnection(@"data source=(local);database=Aukcija;integrated security=true;"); 
    SqlDataAdapter aukcDa = new SqlDataAdapter("select * from auctions", conn); 
    aukcDa.Update(aukcija_bazeDataGrid.DataContext as System.Windows.Forms.DataGrid); 
} 

編輯:以下內容添加到您的button1_click - 方法的窗體2級的:

this.DialogResult = System.Windows.Forms.DialogResult.OK; 
this.Close(); 
+0

這就是說,我不知道更新邏輯的代碼... –

+0

@МаркоЛучић看看我的編輯;) –

+0

代碼的一部分:if(updateForm.ShowDialog .. ... 「updateForm」是什麼意思?就是我的Form2? –

1

對不起,我無法訪問您提供的鏈接。 你必須返回新添加的對象以顯示在mainwindow中,我不知道以什麼方式顯示對象,它必須是我認爲的datagrid,所以在將新創建的對象插入數據庫後,必須添加手動將對象放入數據網格中。因爲datagrid不知道新插入的對象。

您可以嘗試公共屬性,如ITEMNAME,StartPrice,CurrentPrice,在insertingForm,用插入值填充它們,並在主窗口,讓他們並添加到數據網格

這樣

namespace WpfApplication25 { 
public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

public string ItemName{get;set;} 
public string CurrentPrice{get;set;} 
public string StartPrice{get;set;} 

    private void button1_Click(object sender, EventArgs e) 
    { 
     using (SqlConnection connection = new SqlConnection(
        @"data source=(local); 
        database=Aukcija; 
        integrated security=true;")) 
     { 
      DataTable aukcijeTable = new DataTable(); //novo 
      ItemName = textBox1.Text; 
      CurrentPrice = textBox3.Text; 
      StartPrice = textBox2.Text; 
      SqlCommand cmd = new SqlCommand("INSERT INTO Auctions (item_name, start_price, current_price) VALUES (@item_name, @start_price, @current_price)"); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = connection; 
      connection.Open(); 
      cmd.Parameters.AddWithValue("@item_name", textBox1.Text); 
      cmd.Parameters.AddWithValue("@start_price", textBox2.Text); 
      cmd.Parameters.AddWithValue("@current_price", textBox3.Text); 
      cmd.ExecuteNonQuery();   
      connection.Close(); 



     } 
    } 
}} 

,並在主窗口顯示insertng形式獲取屬性值後

namespace WpfApplication25 
{ 
    /// <summary> <br> 
    /// Interaction logic for MainWindow.xaml <br> 
    /// </summary> <br> 
    public partial class MainWindow : Window <br> 
    { 

     int count = 120; 
     System.Windows.Threading.DispatcherTimer tmr = new System.Windows.Threading.DispatcherTimer(); 

private DataTable aukcijeTable;   
public MainWindow() 
     { 
      InitializeComponent(); 

      tmr.Interval = new TimeSpan(0, 0, 1); 
      tmr.Tick += new EventHandler(tmr_Tick); 

      aukcijeTable = new DataTable(); 
      SqlConnection conn = new SqlConnection(@"data source=(local);database=Aukcija;integrated security=true;"); 
      SqlDataAdapter aukcDa = new SqlDataAdapter("select * from auctions", conn); 

      aukcDa.Fill(aukcijeTable); 
      aukcija_bazeDataGrid.DataContext = aukcijeTable; 


     } 



     void tmr_Tick(object sender, EventArgs e) 
     { 
      label1.Content = count -= 1; 
      if (count == 0) 
      { 
       System.Windows.Forms.MessageBox.Show("Auction completed"); 
       tmr.Stop(); 
       count = 120; 
      } 
      else 
      { 


      } 

     } 


     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      Form1 popup = new Form1(); 
      popup.ShowDialog(); 
      popup.Dispose(); 
     } 

     private void button3_Click(object sender, RoutedEventArgs e) 
     { 
      Form2 popup = new Form2(); 
      if(popup.ShowDialog()== DialogResult.OK){ 

       var newRow = aukcijeTable.NewRow(); 
       newRpw[0] = popup.ItemName; 
       newRow[1] = popup.StartPrice; 
       newRow[2] = popup.CurrentPrice; 
       aukcijeTable.Rows.Add(newRow); 
       aukcija_bazeDataGrid.Refresh(); 

      } 

      popup.Dispose(); 
     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      tmr.Start(); 

      using (SqlConnection conn = new SqlConnection(@"data source=(local);database=Aukcija;integrated security=true;")) 

      { 
       DataTable cena1 = new DataTable(); 
       conn.Open(); 
       SqlDataAdapter DA = new SqlDataAdapter(" UPDATE auctions SET current_price = current_price + 1", conn); 
       SqlCommand cmd = new SqlCommand ("UPDATE auctions SET current_price = current_price + 1", conn); 
       DA.Fill(cena1); 
       //DA.Update(cena1); 
       cmd.ExecuteNonQuery(); 
       SqlCommandBuilder cb = new SqlCommandBuilder(DA); //novo 
       DA.Update(cena1); //novo 
       conn.Close(); 

      } 

     } 


     private void aukcija_bazeDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 

     } 

     private void button4_Click(object sender, RoutedEventArgs e) 
     { 

      tmr.Start(); 

     } 

     private void button5_Click(object sender, RoutedEventArgs e) 
     { 
      tmr.Stop(); 
      System.Windows.Forms.MessageBox.Show("Auction completed!"); 
      count = 120; 
     } 


    } 
} 
+0

我已經編輯我的文章,看看照片...我已經試過你的代碼,但仍然是相同的...如果你想,我會給你我的項目。謝謝 –

+0

你可以添加你的主窗口代碼嗎? – VahiD

+0

我已添加主窗口代碼... –

相關問題