检查datagridview中的数据是空还是null

时间:2013-09-14 14:25:21

标签: c# winforms ms-access datagridview

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

这是我的问题:

我想显示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);
                }
            }
        }
    }
}

有谁知道如何修复它?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

仅当您设置DataGridView.CurrentRow时,

DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect 可用。否则你必须得到当前行:

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

更新

看起来您使用DataTable作为dataGridView1的数据源,您应该在Adapter中享受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);
    }

答案 2 :(得分:0)

我发现以下情况非常有用

if(dataGridView1.DataSource!=null)
{
    // do something
}