我正试图将a保存GridView到DataTable.
我有代码,理论上应该这样做但我不断收到此错误:
第0列不存在
这是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
for (int j = 0; j < GridView1.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = GridView1.Rows[j];
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何解决此问题的任何帮助?

正如您从图像中看到的那样,这两个表之间存在一对多关系。但我想让它成为多对多。我使用 AssetID 作为这些关系的关键。关于如何创建这个的任何想法?
之所以需要它作为多对多,是因为我在 powerview 中使用它并使用列标题作为滑块。一个例子是,如果我要在 tblOperatingSystem 滑块中选择 Windows 7,则我用于 tblAssets 的滑块将仅显示与 Windows 7 相关的内容,而我希望能够执行相反的操作并在 tblAssets 滑块中进行选择并且只有与 tblOperatingSystem 滑块相关的操作系统才会出现
我已经尝试创建一个只有 AssetID 的新表,然后将 tblAssets 和 tblOperatingSystem 连接到它,但此方法不适用于滑块。
对此有什么想法吗?