如何在datagridview和mysql数据库中删除一行后刷新datagridview

时间:2014-06-16 23:29:07

标签: mysql datagridview refresh

我有一个带有datagridview的windows c#表单。 datagridgridview链接到一个删除按钮,它成功地从datagridview和附加表单的mysql数据库中删除一行。

不幸的是,删除的行仍显示在datagridview中(同一行从mysql中删除,我通过检查phpMyAdmin确认了。

我使用refesh()方法,但它不起作用。

有人可以看一下我的代码帮我弄清楚我做错了吗? 谢谢, 彼得

 private void deleteCustTrxRow_Click(object sender, EventArgs e)
    {    

        String myConnection = @"server=localhost; database=bea***; username=root; password=*****; convert zero datetime=True";
        MySqlConnection Connect = null;

        try
        {
            Connect = new MySqlConnection(myConnection);
            Connect.Open(); //Open the connection

            if (dataGridViewCalcSalesProductsServicesPurchased.SelectedRows.Count > 0)
            {

                int selectedIndex = dataGridViewCalcSalesProductsServicesPurchased.SelectedRows[0].Index;
                int rowID = int.Parse(dataGridViewCalcSalesProductsServicesPurchased[3, selectedIndex].Value.ToString());

                String deleteQuery = "DELETE FROM line WHERE line_number=@rowID";

                MySqlCommand cmdDeleteFromLineTable = new MySqlCommand(deleteQuery, Connect);

                cmdDeleteFromLineTable.Parameters.AddWithValue("@rowID", rowID);

                MySqlDataReader readTheTable;

                readTheTable = cmdDeleteFromLineTable.ExecuteReader();

                dataGridViewCalcSalesProductsServicesPurchased.Refresh();                                       

            }

            //Inform user that tranaction was successfull
            MessageBox.Show("Record deleted from database");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\nRecord could not be deleted from database\n" + "Please try again");

        }
        finally
        {
            if (Connect != null)
            {
                Connect.Close(); //Close the connection
            }

        }


    }

0 个答案:

没有答案