【发布时间】:2018-10-28 16:04:33
【问题描述】:
我在一个表单上有一些 DataGridViews,并想从中获取一个 XML 字符串。 但由于某种原因,它在返回时给出了“Exception throw: 'System.NullReferenceException'”。 当我查看 foreach 行内部时,它包含我输入的数据。 这里有什么问题?
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();
}
【问题讨论】:
-
您的DataTable
DT没有DataSet。 -
好清楚,所以我填充数据表的方式不会创建数据集。网上搜了下,还是没有办法只保存用户放在DataGridView中的数据,难不成?
标签: c# .net windows winforms visual-studio