[英]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.