我正在使用C#处理Windows窗体应用程序。将过滤的DataGridView分配给DataRow []给出空值(过滤器有结果)
我的表格有DataGridView
您可以添加/删除条目,条目有可编辑的Qty
列,然后是Save
按钮。
一旦点击Save
,我要筛选的DataGridView
条目0.00 Qty
,然后通知用户有/是0.00 Qty
名单上,否则将进行保存。 (见表格的截图点击保存之前)
我有这样下面的代码的形式:
private void SaveBtn_Click(object sender, EventArgs e)
{
if (isWithZeroQty() == true)
{
MessageBox.Show("Please check Quantity","System Alert",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
// Will do the saving..
}
}
private bool isWithZeroQty()
{
DataRow[] result = (enrollmedsDataGridView.DataSource as DataTable).Select("Qty = 0.00 OR Qty = 0");
if (result.Count() > 0)
{ return true; }
else
{ return false; }
}
我的问题是NullReferenceException
发生在这一行:
DataRow[] result = (enrollmedsDataGridView.DataSource as DataTable).Select("Qty = 0.00 OR Qty = 0");
经过我的进一步调查,将DataGridView强制转换为DataTable (enrollmedsDataGridView.DataSource as DataTable)
时发生NullReferenceException。
是因为DataGridView是DataBounded到BindingSource?
如果是这样,我该如何解决这个问题。
在此先感谢您的帮助。