2016-04-29 75 views
0

我正在制作程序以对XML文件数据执行一些功能,然后在网格视图中显示具有正确用户的行,在我的程序中用户可以输入一些值像薪水和程序必须显示在网格视图中有这个数量的工资表中的所有员工,但我怎么能过滤数据使用用户输入,因为“.RowFilter”只取决于我以前决定的价值,如“薪水> 1000 “如何使用用户输入过滤来自XML文件的数据C#

+1

你试过'XmlNodeList中节点列表= root.SelectNodes( “后裔::用户:员工[员工:工资= 10]”,nsmgr);' –

+0

我无法理解你的代码,你能告诉我这意味着什么 –

+0

您可以使用[XPATH](http://www.w3schools.com/xsl/xpath_syntax.asp)读取XML文件user:employee是一个示例路径,指向每个具有父级子级用户的Node员工。 [employee:salary = 10]筛选以获得薪水为10的人...现在,您的所有员工节点都拥有父级用户和10个薪水,您现在可以显示此节点列表 –

回答

0

让我来演示一个如何使用RowFilter的简单示例,它接收来自用户的输入。

using System; 
using System.Data; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      //InitializeComponent(); 
      this.Width = 400; 

      dtPersons = new DataTable(); 
      dtPersons.Columns.Add("Name", typeof(string)); 
      dtPersons.Columns.Add("Age", typeof(int)); 

      // Should be read from file 
      dtPersons.LoadDataRow(new object[] { "John", 30 }, false); 
      dtPersons.LoadDataRow(new object[] { "Smit", 35 }, false); 
      dtPersons.LoadDataRow(new object[] { "Jack", 25 }, false); 
      dtPersons.LoadDataRow(new object[] { "Brad", 20 }, false); 
      dtPersons.LoadDataRow(new object[] { "Paul", 40 }, false); 

      var dgv = new DataGridView { Parent = this, Dock = DockStyle.Top }; 
      dgv.DataSource = dtPersons; 

      tbFilterName = new TextBox { Parent = this, Top = dgv.Bottom + 30, Width = 150 }; 
      nudFilterAge = new NumericUpDown { Parent = this, Top = tbFilterName.Top, Left = tbFilterName.Right + 50 }; 

      var btnFilterName = new Button { Parent = this, Top = dgv.Bottom + 60, Text = "Apply filter by Name", AutoSize = true }; 
      var btnBothFilters = new Button { Parent = this, Top = dgv.Bottom + 60, Text = "Apply both filters", AutoSize = true, Left = btnFilterName.Right + 20 }; 
      var btnFilterAge = new Button { Parent = this, Top = dgv.Bottom + 60, Text = "Apply filter by Age", AutoSize = true, Left = btnBothFilters.Right + 20 }; 

      btnFilterName.Click += BtnFilterName_Click; 
      btnBothFilters.Click += BtnBothFilters_Click; 
      btnFilterAge.Click += BtnFilterAge_Click; 
     } 

     DataTable dtPersons; 
     TextBox tbFilterName; 
     NumericUpDown nudFilterAge; 

     private void BtnFilterAge_Click(object sender, EventArgs e) 
     { 
      string filter = $"Age > '{nudFilterAge.Value}'"; 
      dtPersons.DefaultView.RowFilter = filter; 
     } 

     private void BtnBothFilters_Click(object sender, EventArgs e) 
     { 
      string filter = $"Name = '{tbFilterName.Text}' or Age > '{nudFilterAge.Value}'"; 
      dtPersons.DefaultView.RowFilter = filter; 
     } 

     private void BtnFilterName_Click(object sender, EventArgs e) 
     { 
      string filter = $"Name = '{tbFilterName.Text}'"; 
      dtPersons.DefaultView.RowFilter = filter; 
     } 
    } 
}