开发者

Attempting to build a DataGridView from Xml. Not working

Code is as follows:

string fileName = "passfile.xml";
DataSet ds = new DataSet("Account List");
DataTable accounts = ds.Tables.Add("Accounts");

accounts.Columns.AddRange(new DataColumn[] {
    new DataColumn("Username"),
    new DataColumn("Password"),
    new DataColumn("Description")
);

XmlDocument doc = new XmlDocument();
doc.Load(开发者_StackOverflowfileName);

foreach (XmlNode node in doc.GetElementsByTagName(accountGroupsBox.SelectedItem.ToString()))
{
    DataRow row = accounts.Rows.Add(
        node["Username"].InnerText,
        node["Password"].InnerText,
        node["Description"].InnerText);
}

dataGridView1.DataSource = accounts;

My XML file looks like this:

Well I couldn't figure out how to properly escape the XML, but there there is an element called Account with AccountType with inner text as Email Accounts, or Web Accounts or something similar that matches the items in the combo box. Additionally there are other child elements such as Username, Password, etc.

The problem is when the code actually executes, the DataGridView fills with the proper rows and columns but where is nothing in them....what did I do wrong?


After you assign your DataSource, you need to call DataBind().

dataGridView1.DataSource = accounts;
dataGridView1.DataBind();

You should also be able to bind directly to the XML. Something like this...

DataSet ds = new DataSet();
ds.ReadXml("passfile.xml",XmlReadMode.InferSchema);
dataGridView1.DataSource = ds;
dataGridView1.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜