1

我有一个问题,也许这个论坛的你们可以帮助我。

这是我的问题:

我想显示 MessageBox 说 datagridview 中没有数据,你不能删除它。我已经可以删除datagridview中的数据,但是当datagridview包含0数据时,我点击删除“按钮”,这是错误的。错误是:Object reference not set to an instance of an object. NullReferenceException

这是错误指向的代码: int rowNum = dataGridView1.CurrentRow.Index;

这是代码:

private void Delete(object sender, EventArgs e)
{
    DataTable dt = (DataTable) dataGridView1.DataSource;
    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();
        }

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

            if (rowNum == 0)
            {
                bool rowIsEmpty = true;

                foreach (DataGridViewCell cell in dataGridView1.CurrentRow.Cells)
                {
                    if (cell.Value != null)
                    {
                        rowIsEmpty = false;
                        break;
                    }
                }

                if (rowIsEmpty)
                {
                    System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                    sounds.Play();
                    MessageBox.Show("Tidak ada Data di Baris ini!", "Error");
                }
                else
                {
                    Delete(sender, e);
                }
            }
        }
    }
}

有谁知道如何解决它?

4

3 回答 3

1

像这样尝试 dt.Rows.count > 0 表示数据表中有数据,如果没有数据表中没有数据,如果数据存在,您可以进行操作。dt.Rows.count 将给出数据表中的行数

于 2013-09-14T16:18:48.340 回答
0

DataGridView.CurrentRow仅在设置时可用DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect。否则,您必须通过以下方式获取当前行:

int rowNum = dataGridView1.CurrentCellAddress.Y;
var currentRow = dataGridView1.Rows[rowNum];

更新

看起来您使用 aDataTable作为 DataSource dataGridView1,您应该受益于Adapterin ADO.NET。此代码只是对您当前代码的一些修改,实际上它分两个阶段更新数据: 1. 从 DataTable 和 DataGridView 中删除行 2. 删除数据库中的实际行。

    private void Delete(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)dataGridView1.DataSource;
        int rowNum = dataGridView1.CurrentCellAddress.Y;
        if(rowNum == -1) {
           MessageBox.Show("There is no row selected!");
           return;
        }
        int id = Convert.ToInt32(dt.DefaultView[rowNum]["ID"]);
        //check if row is empty, simply return
        if(IsRowEmpty(rowNum)){
          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;
        }
        //Remove the row
        dt.DefaultView[rowNum].Delete();
        dt.AcceptChanges(); //<-- Because you don't use Adapter, call this to restore the row state.
        //Remove the underlying row in database
        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();
            }

            if (choice.comboBox1.Text == "English")
            {
                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                sound.Play();
                MessageBox.Show("Deleted Successfully!", "Deleted");                    
            }
         }
    }
    //method to check if a row is empty
    private bool IsRowEmpty(int index){
       return dataGridView1.Rows[index].Cells.OfType<DataGridViewCell>()
                                       .All(c=>c.Value == null);
    }
于 2013-09-14T14:29:49.030 回答
0

我发现以下条件非常有用

if(dataGridView1.DataSource!=null)
{
    // do something
}
于 2016-04-27T10:38:53.290 回答