如何在C#中的运行时将数据从Xml文件添加到Datagridview? [英] How Do I Add Data From A Xml File To A Datagridview At Runtime In C#?
问题描述
我在DataGridView中有四行,datagridview的结构已定义为
HOSTNAME IP_ADDRESS USERNAME PASSWORD
我正在从xml文件加载数据,文件看起来像这样
Hi,
I have four rows in a DataGridView and the structure of the datagridview is already defined as
HOSTNAME IP_ADDRESS USERNAME PASSWORD
I am loading the data from a xml file and the file looks like this
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table1>
<Host-Name>ixin-sovit</Host-Name>
<Ip_Address>10.205.29.19</Ip_Address>
<User_Name>sovit</User_Name>
<Password>12345</Password>
</Table1>
</NewDataSet>
当我在加载时加载表单时, datagridview看起来像这样
HOSTNAME IP_ADDRESS USERNAME PASSWORD
12345
其余的列没有填充
我正在使用的代码是
When i am loading the form at load time, the datagridview looks like this
HOSTNAME IP_ADDRESS USERNAME PASSWORD
12345
Rest of the columns are not getting populated
The code i am using is
if (File.Exists(@"D:\HostAdded.xml") && new FileInfo(@"D:\HostAdded.xml").Length != 0)
{
dataGridView1.DefaultCellStyle.ForeColor = Color.Red;
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"D:\HostAdded.xml");
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dataSet;
dataGridView1.DataMember = "Table1";
this.Cursor = Cursors.Default;
}
我无法理解问题出在哪里?
期待您的回复!!!
谢谢:)
I am unable to understand where the problem is??
Looking forward to your response!!!
Thanks :)
推荐答案
转到您的表格.Designer.cs
文件并检查为每列提供的DataPropertyName
,您需要为每列提供以下内容
Go to Your form.Designer.cs
file and check forDataPropertyName
given for each column, you need to give below for each column
Host-Name
Ip_Address
User_Name
Password
这篇关于如何在C#中的运行时将数据从Xml文件添加到Datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!