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;
}
}
You should use this instead of setting DataSource to null:
dgvDeposits.DataSource = typeof(Deposit);
Please check following question it might have an explanation for your exception.
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.
DataTable dt = new DataTable();
dt.Columns.Add("DepAmt", typeof(double));
dt.Columns.Add("DepDate", typeof(DateTime));
dt.Columns.Add("DepositId", typeof(int));
dgvDeposits.DataSource = dt;
This site is what I referenced.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.