c#winform,不能从dbnull到其他类型的对象
我的数据杂志中有复选框,可以从已经从数据库中加载的数据中删除一个或多个行。 DataSource是DataBindingSource。 我想首先检查用户是否检查了一个或多个复选框,这是我要检查的代码。
int total = dgvRegister.Rows.Cast<DataGridViewRow>().Where(x => Convert.ToBoolean(x.Cells["Delete"].Value) == true).Count();
if (total > 0)
{
// Do something
}
但是我遇到了这个错误“对象不能从dbnull施放到其他类型”。我很高兴,但没有帮助。 请帮助我解决这个问题,并提前非常感谢。
这是孔代码
private void btnDeActivateComputers_Click(object sender, EventArgs e)
{
try
{
using (CompEntities db = new CompEntities())
{
if (dgvRegister.Rows.Count > 0)
{
int total = dgvRegister.Rows.Cast<DataGridViewRow>().Where(x => Convert.ToBoolean(x.Cells["Delete"].Value) == true).Count();
if (total > 0)
{
if (MessageBox.Show("Do you want to delete?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
for (int i = dgvRegister.RowCount - 1; i >= 0; i--)
{
DataGridViewRow row = dgvRegister.Rows[i];
if (Convert.ToBoolean(row.Cells["Delete"].Value) == true)
{
int ComId = Convert.ToInt32(dgvRegister.Rows[row.Index].Cells[0].Value.ToString());
var deletecomp = db.Comp.Where(x => x.ComId == cId).FirstOrDefault();
if (deletecomp != null)
{
db.Comp.Remove(deletecomp);
}
db.SaveChanges();
}
}
}
}
else
{
MessageBox.Show("Please Check at least one checkbox !", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Exception inner = ex.InnerException;
while (inner != null)
{
MessageBox.Show(inner.Message);
inner = inner.InnerException;
}
}
}
I have checkbox in my DataGridView to delete one or multiple rows from my DataGridView already loaded data from database. DataSource is dataBindingSource.
I want to check first if the user checked one or multiple checkboxes and here is my code to check.
int total = dgvRegister.Rows.Cast<DataGridViewRow>().Where(x => Convert.ToBoolean(x.Cells["Delete"].Value) == true).Count();
if (total > 0)
{
// Do something
}
But I'am geting this error "object cannot be cast from dbnull to other types". I have gogled but not s omuch to help.
Please help me to solv this problem and thanks a lot in advance.
Here is the hole code
private void btnDeActivateComputers_Click(object sender, EventArgs e)
{
try
{
using (CompEntities db = new CompEntities())
{
if (dgvRegister.Rows.Count > 0)
{
int total = dgvRegister.Rows.Cast<DataGridViewRow>().Where(x => Convert.ToBoolean(x.Cells["Delete"].Value) == true).Count();
if (total > 0)
{
if (MessageBox.Show("Do you want to delete?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
for (int i = dgvRegister.RowCount - 1; i >= 0; i--)
{
DataGridViewRow row = dgvRegister.Rows[i];
if (Convert.ToBoolean(row.Cells["Delete"].Value) == true)
{
int ComId = Convert.ToInt32(dgvRegister.Rows[row.Index].Cells[0].Value.ToString());
var deletecomp = db.Comp.Where(x => x.ComId == cId).FirstOrDefault();
if (deletecomp != null)
{
db.Comp.Remove(deletecomp);
}
db.SaveChanges();
}
}
}
}
else
{
MessageBox.Show("Please Check at least one checkbox !", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Exception inner = ex.InnerException;
while (inner != null)
{
MessageBox.Show(inner.Message);
inner = inner.InnerException;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个使用实体框架核心和C#9的模式,如果使用.NET Framework 4.8,则需要进行一些调整,但是原理与下面指出的相同。
模型
类别
其中检查
属性不是基础表的一部分。[notmapped]
告诉Entity框架在[DefaultValue时都排除此属性(false)]
提供false作为默认值。在表单中,创建一个
bindingsource
,用于所有操作,曾经分配为DataGridView的数据源。如各种注释中所述,需要通过DataGridView引用数据。注释
表单代码的属性,其中有两个属于自己的文件中的类。
Here is a pattern to following using Entity Framework Core and C#9, some adjustments are needed if using say .NET Framework 4.8 but the principles are the same as noted below.
Model
Categories
whereChecked
property is not part of the underlying table.[NotMapped]
tells Entity Framework to exclude this property while[DefaultValue(false)]
provides false as the default value.In the form, create a
BindingSource
which is used for all operations once assigned as the DataSource to the DataGridView. As mentioned in various comments, there is zero need to reference data via the DataGridView.Notes
Form code with two classes that belong in their own files.