0

我正在为拍卖申请 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它时,我会打开一个新表格,并用新的拍卖信息填写它,当我点击确定时,什么也没有发生。我必须关闭我的应用程序并再次打开它才能显示数据库中新插入的记录。

我在代码中遗漏了什么?当我按下确定时,我需要一些东西来自动刷新(更新)主窗口......

4

2 回答 2

2

在您的MainForm(您打开更新表单的位置)中,将您当前打开更新表单的部分替换为以下代码:

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_clickForm2 类的 -method 中:

this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
于 2013-09-23T09:58:05.887 回答
1

抱歉,我无法访问您提供的链接。您必须以一种方式返回新添加的对象才能在主窗口中显示,我不知道您以哪种方式显示对象,我认为它必须是datagrid,所以在将新创建的对象插入数据库后,您必须添加该对象手动进入数据网格。因为 datagrid 不知道新插入的对象。

您可以在 insertingForm 上尝试 ItemName、StartPrice、CurrentPrice 等公共属性,用插入值填充它们,然后在主窗口中获取它们并添加到数据网格中

像这样

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



        }
    }
}}

并在显示插入表单后的主窗口中获取属性值

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


    }
}
于 2013-09-23T09:54:27.243 回答