如何在网格视图C#上显示XML文件的结果?

时间:2016-04-28 14:11:20

标签: c# xml winforms

`这是我的代码的一部分:

        if (radioButton2.Checked)
        {
            int gthan = int.Parse(textBox2.Text);
            foreach (XmlNode n in doc.SelectNodes("/employees/employee/salary"))
            {

                int curr = Int32.Parse(n.InnerText);
                if (curr <gthan)
                { noe++;
                }
            }
            MessageBox.Show("number of employees" + +noe);}

` 我在XML文件表中执行了一些函数,比如带来最高工资等但是如何告诉程序在windows窗体应用程序中显示网格视图中具有最大工资的子项?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作

var ds = new DataSet();
ds.ReadXml("Path to your xml file");
dgvSalary.DataSource = ds.Tables[0];
dgvSalary.Refresh();    

在此示例中,您希望在Class级别声明DataTable

//dtSearch is a DataTable that was declared at the class level as 
public DataTable dtSearch = null; 
//you can populate the dtSearch with the ds.Tables[0];
dtSearch = ds.Tables[0];
dv = new DataView(dtSearch); //create a DataView in memory
dv.RowFilter = "Salary < 1000";
//bind the DataGridView to dv 
dgvSalary.DataSource = dv;
dgvSalary.Refresh();