c# - DataGridView.Datasource = null;错误: Object reference not set to an instance of an object

标签 c# datagridview

我很困惑为什么将 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;            
    }
}

最佳答案

您应该使用它而不是将 DataSource 设置为 null:

dgvDeposits.DataSource = typeof(Deposit);

请检查以下question它可能对您的异常有解释。

关于c# - DataGridView.Datasource = null;错误: Object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867136/

相关文章:

C#:DataGridView 控件中的多行文本

c# - 将二维集合绑定(bind)到 DataGridView

c# - 如何在 C# 中以编程方式将 xlsx 文件转换为 2003 xls 文件?

c# - Gmail 的 SmtpClient

c# - 如何找出 .net 类实现了哪些接口(interface)?

c# - 获取 Excel 工作表名称到 C#

c# - Entity Framework 不断尝试连接到旧的 ADO.NET 提供程序,即使它已更改

c# - 如何使用 LinkedIn REST API 发帖到群组?

mysql - 数据 GridView 显示列但不显示数据

c# - ADO.NET 从 datagrid1 中选择一行以显示 datagrid2 中的行