[英]Check if the data in datagridview is empty or null
我有一個問題,可能這個論壇的大家可以幫我。
這是我的問題:
我想顯示MessageBox,說datagridview中沒有數據,您不能刪除它。 我已經可以刪除datagridview中的數據,但是當datagridview包含0個數據,並且我單擊刪除“按鈕”時,這是錯誤的。 錯誤是: Object reference not set to an instance of an object. NullReferenceException
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);
}
}
}
}
}
有誰知道如何解決?
像這樣嘗試dt.Rows.count> 0表示如果數據表中沒有數據,則數據表中有數據,如果存在數據,則可以進行操作。 dt.Rows.count將給出數據表中的行數
DataGridView.CurrentRow
僅當設置為可用 DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect
。 否則,您必須通過以下方式獲取當前行:
int rowNum = dataGridView1.CurrentCellAddress.Y;
var currentRow = dataGridView1.Rows[rowNum];
看起來您使用DataTable
作為dataGridView1
的DataTable
源,應該利用ADO.NET
的Adapter
的好處。 該代碼只是修改您當前代碼的一部分,實際上可以分兩個階段更新數據: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);
}
我發現以下條件非常有用
if(dataGridView1.DataSource!=null)
{
// do something
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.