简体   繁体   中英

Refresh DataGridView with updated rows C#

I have a 'Create new Order' Form where I create an order which once created in now on the DB Table displayed in a datagridView. However I am struggling to get the datagridView to auto update with the new data in it.

This is the form: 在此处输入图像描述

This is the table where the new order should be displaying. However only displays when i switch tables and not when the New order is created successfully.

在此处输入图像描述

I have tried the dataGridView.Refresh() and a bunch of other researched solutions but non seem to work. This is my Code.

MySqlConnection conn = new MySqlConnection();
      conn.Close();
      var x = inputOrderTitle.Text;
      Random rnd = new Random();

      try
      {
        
        MySqlConnection mysqlConnection = new MySqlConnection();
        connect.OpenSuccessfulDBConnection(mysqlConnection);

        String query = "INSERT INTO tb_orders VALUES (@order_id, @title, @description, @scheduled_date, @deadline_date, @word_count, @editor_url, @status, @is_complete, @is_invoiced, @is_closed, @type, @ClientID, @InvoiceID, is_inprogress, @client_name, @order_cost)";

        MySqlCommand cmd = new MySqlCommand(query, mysqlConnection);
        cmd.Parameters.AddWithValue("@order_id", "BLG0556");
        cmd.Parameters.AddWithValue("@title", "abc");
        cmd.Parameters.AddWithValue("@description", "abc");
        cmd.Parameters.AddWithValue("@scheduled_date", dateTimeSchedDate.Value.Date);
        cmd.Parameters.AddWithValue("@deadline_date", dateTimeSubDate.Value.Date);
        cmd.Parameters.AddWithValue("@word_count", 123);
        cmd.Parameters.AddWithValue("@editor_url", "abc");
        cmd.Parameters.AddWithValue("@status", "abc");
        cmd.Parameters.AddWithValue("@is_complete", 0);
        cmd.Parameters.AddWithValue("@is_invoiced", 0);
        cmd.Parameters.AddWithValue("@is_closed", 0);
        cmd.Parameters.AddWithValue("@type", comboBoxOrderType.Text);
        cmd.Parameters.AddWithValue("@ClientID", 123);
        cmd.Parameters.AddWithValue("@InvoiceID", 432);
        cmd.Parameters.AddWithValue("@is_inprogress", 1);
        cmd.Parameters.AddWithValue("@client_name", "abc");
        cmd.Parameters.AddWithValue("@order_cost", 30.00);

        int result = cmd.ExecuteNonQuery();

        Application.OpenForms
       .OfType<Form>()
       .Where(form => String.Equals(form.Name, "NewOrder"))
       .ToList()
       .ForEach(form => form.Close());

        MessageBox.Show("Order created successfully.");

        orders.dataGridViewOrderList.Rows.Clear();
        connect.GetOrderList(orders.dataGridViewOrderList);
      }

Use the 'DataSource' property to bind the grid to the bunch of fetched data

            SqlConnection mysqlConnection = new SqlConnection("your connection string");
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tb_orders(nolock)", mysqlConnection);

            if (mysqlConnection.State == ConnectionState.Closed)
                mysqlConnection.Open();

            da.SelectCommand.ExecuteNonQuery();
            da.Fill(dt);

            dataGridViewOrderList.DataSource = dt;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM