2016-04-29 21 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; 
     } 
    } 
} 
相關問題