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;
}
}
dgvDepositsis notnull? It might be that you are just barking up the wrong tree. I've never worked withDataGridViewthough and it might really be the implemented behaviour to throw an exception if you passnullto the setter.dgvDepositsis not null in line you've marked. There is nothing wrong with settingDataGridView.Datasource = null- I've decompiled it's setter and it doesn't throw any exception ifDatasourceis null. Maybe you're wired with some additional event, likeDataSourceChanged, where the exception is thrown?DataSourceproperty that fails. So for example, try adding the rowdgvDeposits.MultiSelect = false;before the line setting theDataSource. If that fails, try looking here and comparing with your own code to make sure it's initialized properly.