オークション用のwpfアプリケーションを作成しています。メインウィンドウは次のようになります: http://www54.zippyshare.com/v/91622733/file.html
メインウィンドウのコード:
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 .
その新しいフォームのコード:
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
と新しいフォームが開き、新しいオークション情報を入力します。[OK] をクリックしても何も起こりません。データベースから新しく挿入されたレコードを表示するには、アプリケーションを閉じてから再度開く必要があります。
コードに何が欠けていますか? OKを押したときにメインウィンドウを自動的に更新(更新)するものが必要です...