感謝您的時間。在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)
嗨,謝謝你的幫助。我嘗試你的代碼,但不幸的是它沒有工作。 'code' DataView dv =((DataTable)DataGridValue.DataSource).DefaultView; dv.RowFilter =「Northing tbxX1.Text」; '代碼'但錯誤出來了,它無法找到我的文本框的列名稱。任何建議? –
Momento
嘗試**'dv.RowFilter = String.Format(「Nothing <'{0}'AND Nothing>'{1}'」,tbxX2.Text,tbxX1.Text)「** - 如果您嘗試像您一樣寫在你的評論然後它沒有與實際的文字比較,但試圖比較一個名爲「tbxX2.Text」,它沒有找到。 – therak
它的工作!!!非常感謝你therak。你的幫助是非常非常感謝。謝謝。 – Momento