使用两个文本框内的值过滤DataGridView列中的数据行值

感谢您的时间。

我正在尝试编写程序来过滤CSV文件中的值。 我的表单中有三个textbox和一个datagridview。

到目前为止,我设法将CSV解析为DataGridView。 当我尝试通过在文本框中使用两个值来过滤第一列内的值时,会出现问题。

到目前为止,我只是设法选择了文本框中给出的值的行。 如何过滤datagridview,如下所示: –

Textbox1 value  Textbox2 value 

这是我的csv文件的示例: –

  Northing,Easting,Result 645789.900,578778.982,6.78 645782.892,578767.289,5.54 645801.435,579213.430,6.78 645804.156,579445.670,5.79 645980.188,582544.389,8.90 645983.456,582667.344,8.79 646590.253,584788.212,7.60 646800.789,585690.312,2.50 646909.452,585780.212,4.30 647900.323,585890.345,6.89 

这是我目前使用的代码: –

  using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Data; using System.IO; using System.Linq; using System.ComponentModel; using DgvFilterPopup; namespace ZoningParameter { ///  /// Description of MainForm. ///  public partial class MainForm : Form { public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer // InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } void BtnSelectClick(object sender, EventArgs e) { Stream myStream; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = openFileDialog1.OpenFile()) != null) { // Insert code to read the stream here. tbxSelectFile.Text = openFileDialog1.FileName; myStream.Close(); } } } void BtnCancelClick(object sender, EventArgs e) { tbxSelectFile.Text = null; } void BtnGenerateClick(object sender, EventArgs e) { // get all lines of csv file string[] str = File.ReadAllLines(tbxSelectFile.Text); // create new datatable DataTable dt = new DataTable(); // get the column header means first line string[] temp = str[0].Split(','); // creates columns of gridview as per the header name foreach (string t in temp) { dt.Columns.Add(t, typeof(string)); } // now retrive the record from second line and add it to datatable for (int i = 1; i < str.Length; i++) { string[] t = str[i].Split(','); dt.Rows.Add(t); } DataGridValue.DataSource = dt; } void BtnFilterClick(object sender, EventArgs e) { // create new DataTable DataTable dt = ((DataTable)DataGridValue.DataSource); foreach (DataGridViewRow row in DataGridValue.Rows) { // Test if the first column of the current row equals // the value in the text box if ((String)row.Cells["Northing"].Value == tbxX1.Text) { // we have a match row.Selected = true; } else { row.Selected = false; } } 

有人能告诉我如何做到这一点的正确方法吗? 非常感谢你。

EDITED!

谢谢therak的帮助。 这是工作代码

  dv.RowFilter = String.Format("Northing  '{1}'",tbxX2.Text, tbxX1.Text) 

您正在寻找的是一个可以绑定到DataGridView.DataSource的DataView 。 可以对DataView进行过滤/排序等。有关DataView的更多信息,请参阅此处

在你的情况下,它将是这样的:

 DataView dv = ((DataTable)DataGridValue.DataSource).DefaultView; dv.RowFilter = "ColumnName < TB1 AND ColumName > TB2" Afterwards bind the DataView to your gridView 

试试这样

 private void button1_Click(object sender, EventArgs e) { (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Northing = '{0}'", textBox2.Text); } 

尝试这没有测试它,但可能适合您的情况:

 void BtnFilterClick(object sender, EventArgs e) { // create new DataTable DataTable filteredDataTable = new DataTable(); filteredDataTable.Columns.Add("csv column"); DataTable dt = ((DataTable)DataGridValue.DataSource); foreach (DataGridViewRow row in DataGridValue.Rows) { // Test if the first column of the current row equals // the value in the text box if ((String)row.Cells["Northing"].Value == tbxX1.Text) { // we have a match filteredDataTable.Add(row) } } DataGridValue.DataSource = null; DataGridValue.DataSource = filteredDataTable; }