2014-07-21 18 views
0

感謝您的時間。在DataGridView中用兩個文本框中的值過濾數據行值

我試圖編寫程序來過濾CSV文件中的值。我的表單中有三個文本框和一個datagridview。

到目前爲止,我設法將CSV解析到DataGridView中。當我嘗試通過在文本框中使用兩個值來過濾第一列內的值時,會發生問題。

到目前爲止,我只是設法選擇了在文本框中給出的值的行。我怎麼可能過濾datagridview的象下面這樣: -

Textbox1 value < value in column > 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 
{ 
    /// <summary> 
    /// Description of MainForm. 
    /// </summary> 
    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 !!!

謝謝你的幫助。這是工作代碼

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

回答

1

你正在尋找的是可以綁定到一個DataGridView.DataSource DataView。 DataViews的可過濾/排序等有關數據視圖的詳細信息必須在this

在你的情況下,戰利品將是這樣的:

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

嗨,謝謝你的幫助。我嘗試你的代碼,但不幸的是它沒有工作。 'code' DataView dv =((DataTable)DataGridValue.DataSource).DefaultView; dv.RowFilter =「Northing tbxX1.Text」; '代碼'但錯誤出來了,它無法找到我的文本框的列名稱。任何建議? – Momento

+0

嘗試**'dv.RowFilter = String.Format(「Nothing <'{0}'AND Nothing>'{1}'」,tbxX2.Text,tbxX1.Text)「** - 如果您嘗試像您一樣寫在你的評論然後它沒有與實際的文字比較,但試圖比較一個名爲「tbxX2.Text」,它沒有找到。 – therak

+0

它的工作!!!非常感謝你therak。你的幫助是非常非常感謝。謝謝。 – Momento

0

嘗試像這樣

private void button1_Click(object sender, EventArgs e) 
{ 


    (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = 
    string.Format("Northing = '{0}'", textBox2.Text); 

} 
+0

嗨,謝謝你的幫助。我嘗試你的陳述,它只產生與文本框相同的值。任何想法如何檢索兩個文本框中的兩個值之間的值的範圍?謝謝 – Momento

0

試試這個沒有測試過,但可能工作爲你的情況:

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; 

} 
+0

嗨,感謝您的幫助。結果,輸出產生的單一值只與我在tbxX1.Text中輸入的值相同。但是我想在兩個文本框中產生兩個值之間的任何值。 ? – Momento

相關問題