DataGridView.Datasource = null;错误:未将对象引用设置为对象的实例
我很困惑为什么将 datagridview 控件的数据源设置为 null 会导致“对象引用未设置到对象的实例”错误。提前致谢
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "deposits"))
{
oDeposit.DepAmt = Convert.ToDouble(xmlReader.GetAttribute("depamount"));
oDeposit.DepDate = Convert.ToDateTime(xmlReader.GetAttribute("depdate"));
oDeposit.DepositId = Convert.ToInt32(xmlReader.GetAttribute("depid"));
oCustomer.addDeposits(oDeposit);
**dgvDeposits.DataSource = null;**
dgvDeposits.DataSource = oCustomer.Deposits;
}
}
I'm confused as to why setting the datasource of a datagridview control to null, would cause an "object reference not set to an instance of an object" error. Thanks in advance
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "deposits"))
{
oDeposit.DepAmt = Convert.ToDouble(xmlReader.GetAttribute("depamount"));
oDeposit.DepDate = Convert.ToDateTime(xmlReader.GetAttribute("depdate"));
oDeposit.DepositId = Convert.ToInt32(xmlReader.GetAttribute("depid"));
oCustomer.addDeposits(oDeposit);
**dgvDeposits.DataSource = null;**
dgvDeposits.DataSource = oCustomer.Deposits;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用此选项,而不是将 DataSource 设置为 null:
请检查以下可能存在的问题对您的例外情况的解释。
You should use this instead of setting DataSource to null:
Please check following question it might have an explanation for your exception.
好吧,我知道我对此很陌生,但我也遇到了同样类型的问题。我发现使用 DataGridView 中的列创建数据表,然后将该表设置为数据源可以解决问题。
这个网站是我引用的。
Ok, So I know I'm new to this, but I had the same type of problem. I found that creating a DataTable using the Columns in the DataGridView then setting the table as the DataSource fixes the problem.
This site is what I referenced.