0

我已经可以删除数据库并希望在所选行(datagridview)中没有数据时显示错误,但它仅在用户不输入任何数据(或datagridview仍然有空行)时才第一次工作。

private void DeleteDatabase(object sender, EventArgs e)
{
    DataTable dt = (DataTable)dataGridView1.DataSource;

    if (dt.Rows.Count > 0)
    {
        int rowNum = dataGridView1.CurrentRow.Index;
        int id = Convert.ToInt32(dt.DefaultView[rowNum]["ID"]);
        dt.DefaultView[rowNum].Delete();

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            string query = "DELETE FROM [Table] WHERE [ID] = @ID";
            conn.Open();

            using (OleDbCommand cmd = new OleDbCommand(query, conn))
            {
                cmd.Parameters.AddWithValue("@ID", id);
                cmd.ExecuteNonQuery();
            }

            conn.Close();
        }
    }

    else if (dt.Rows.Count <= 0)
    {
        if (choice.comboBox1.Text == "English")
        {
            System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
            sounds.Play();
            MessageBox.Show("There is no Data in the Selected Row!", "Error");
            return;
        }
    }

    if (choice.comboBox1.Text == "English")
    {
        System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
        sounds.Play();
        MessageBox.Show("Deleted Successfully!", "Deleted");
    }
}

当用户输入数据时,将其删除。数据删除成功,但是当用户删除数据后再次点击“删除”按钮时,出现如标题所示的错误。

以下是屏幕截图(我将 3 个屏幕截图混合到 1 个图像中):

在此处输入图像描述

----截图一说明(左图)-----:

当我在上午 12:38 运行程序时,当我点击“删除”按钮时,它给出了如上面的屏幕截图(1)(注意:它是正确的)。

-----截图2说明(中图)-----:

程序在上午 12:39 仍然处于活动状态,一切正常(数据已添加,“删除”功能运行,因为所选行中有数据)。

----截图3说明(右图)-----:

程序在上午 12:40 仍然处于活动状态,之前的数据(参见屏幕截图 2)已被删除,所选行再次为空,当我单击“删除”按钮时,消息框没有像屏幕截图 1 那样显示,但是它给出了同样的错误。(这是我面临的问题)。

注意:抱歉,图片尺寸过小。

问题是:为什么当用户没有输入任何数据时,There is no data in the selected row当用户单击“删除”按钮时会运行消息框。但是当用户输入任何数据时(假设数据已经输入到第一行,并且第一行的选择将是整行)并单击“删除”按钮,它会删除所选行中的数据,当用户再次单击“删除”,它没有显示 的​​消息框There is no data in the selected row,但是它显示了像标题一样的错误。

4

1 回答 1

1

您不需要else if.

尝试:

private void DeleteDatabase(object sender, EventArgs e)
{
    DataTable dt = (DataTable)dataGridView1.DataSource;

    if (dt.Rows.Count > 0)
    {
        int rowNum = dataGridView1.CurrentRow.Index;
        int id = Convert.ToInt32(dt.DefaultView[rowNum]["ID"]);
        dt.DefaultView[rowNum].Delete();

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            string query = "DELETE FROM [Table] WHERE [ID] = @ID";
            conn.Open();

            using (OleDbCommand cmd = new OleDbCommand(query, conn))
            {
                cmd.Parameters.AddWithValue("@ID", id);
                cmd.ExecuteNonQuery();
            }

            conn.Close();
        }

        if (choice.comboBox1.Text == "English")
        { 
            System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
            sounds.Play();
            MessageBox.Show("Deleted Successfully!", "Deleted");
        }
    }
    else // count is 0
    {
        if (choice.comboBox1.Text == "English")
        {
            System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
            sounds.Play();
            MessageBox.Show("There is no Data in the Selected Row!", "Error");
            // return; (this return is redundant as we're at the end of the method.)
        }
    }
}

你的问题也可能是你打电话的地方DeleteDatabase()

于 2013-09-15T18:21:13.200 回答