Hi,
Thank you for your time. I kindly ask your assist to rectify the problem regarding with my code. Basically, my program is to retrieve the value from CSV file and export to DataGridView.
Then i try to retrieve the rows value in column [0] and filter it using two textbox value.
( Textbox1.value <= Columns[0].Value <= Textbox2.value)
The problem is i try to using the rowfilter but its unsuccessfully.
Here is my code:-
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;
namespace FilterCSV
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
DgvFilterManager filterManager = new DgvFilterManager(DataGridValue);
//
// 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)
{
(DataGridValue.DataSource as DataTable).DefaultView.RowFilter = string.Format("Northing = '{0}'", tbxX1.Text);
}
}
}And this is my sample 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
Please show me how the right way to do it. Thank you so much.