本文介绍了如何解决错误“字段的子列表< TableName>无法创建。 "在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格中的DataGridView,在加载表单时从XML文件加载。每次将新行添加到datagridview时,都会更新此XML文件。

最初它工作正常,但现在它显示以下错误:

无法创建字段< tablename>的子列表。其中< tablename>是桌子的名字。



我正在做的事情:



 DataSet dataSet =  new  DataSet(); 
dataSet.ReadXml( @ D:\ HostAdded.xml);
DataRow row = dataSet.Tables [ 0 ]。NewRow();
row.ItemArray = new object [] {txthostname.Text,ipAddr,txtusername.Text ,txtpassword.Text};
dataSet.Tables [ 0 ]。Rows.Add(row);
dataGridView1.DataSource = dataSet;
dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
dataGridView1.AutoGenerateColumns = false ;
txtLog.AppendText(Environment.NewLine + \ nRow添加成功);
createXML();





createXML()

 DataTable dt =  new  DataTable(); 
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataRow dRow = dt.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
dRow [cell.ColumnIndex] = cell.Value;
}
dt.Rows.Add(dRow);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml( @ D:/HostAdded.xml);





请帮助!!!

谢谢:)

解决方案

Hi, i have DataGridView in a form which is loaded from a XML file when the form is loaded. This XML file is updated every time a new row is added to the datagridview.
Initially it was working fine but now it is showing the following error :
"Child list for field <tablename> cannot be created." where <tablename> is the name of the table.

This is what i am doing :

DataSet dataSet = new DataSet();
dataSet.ReadXml(@"D:\HostAdded.xml");
DataRow row = dataSet.Tables[0].NewRow();
row.ItemArray = new object[] { txthostname.Text, ipAddr, txtusername.Text, txtpassword.Text };
dataSet.Tables[0].Rows.Add(row);
dataGridView1.DataSource = dataSet;
dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
dataGridView1.AutoGenerateColumns = false;
txtLog.AppendText(Environment.NewLine + "\nRow added successfully");
createXML();



createXML()

DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                dt.Columns.Add(col.HeaderText);
            }
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataRow dRow = dt.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                dt.Rows.Add(dRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.WriteXml(@"D:/HostAdded.xml");



Please help!!!
Thanks :)

解决方案


这篇关于如何解决错误“字段的子列表&lt; TableName&gt;无法创建。 &QUOT;在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:51
查看更多