I have some DataGridViews on a form, and would like to get a XML string from it. But for some reason it gives "Exception thrown: 'System.NullReferenceException'" at the point of return. When I look inside the foreach rows, it has the data I have put in. What is the problem here?
public string DataGridViewToXML(DataGridView DGV)
{
DataTable DT = new DataTable();
foreach (DataGridViewColumn col in DGV.Columns) { DT.Columns.Add(col.Name); }
foreach (DataGridViewRow row in DGV.Rows)
{
DataRow dRow = DT.NewRow();
foreach (DataGridViewCell cell in row.Cells) { dRow[cell.ColumnIndex] = cell.Value; }
DT.Rows.Add(dRow);
}
return DT.DataSet.GetXml();
}