Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# filter a DataGridView filled from csv file...

sobrano

New Coder
Hello,
I filled up a DataGridView from a csv file using the code as follow:

Everything works and at the start I can see my DataGridView correctly filled up by datas from csv file.
The problem is that I am not able to filter the rows by textbox or sort the column by clicking on header word.
I tried differents ways by reading solution on the web but nothings works...
can anyone suggest me some solution for this pls?

C#:
private void LoadDatabaseBtn_Click(object sender, EventArgs e)
        {
            string filecsv = @"D:\Database.csv";
            DGV.DataSource = LoadCSV(filecsv);
            DGV.Refresh();
        }
        public List<Client> LoadCSV(string csvfile)
        {
           var query = from l in File.ReadAllLines(csvfile)
                        let data = l.Split(',')
                        select new Client
                        {
                            name= data[0],
                            id = data[1],
                            tel = data[2],
                            addr = data[3],
                            city = data[4],                         
                        };
            return query.ToList();
        }
    }
    public class Client
    {
        public string name { get; set; }
        public string id { get; set; }
        public string tel { get; set; }
        public string addr { get; set; }
        public string city { get; set; }   
    }

I have an identical database .mdf, in this case I added a DataSets to my c# project and I fill up my DataGridView just by setting the ClientsTableBindingSouce as source of data
In this case I am able to sort and filter very easly the rows of the datagridview at runtime just by typing text in a textbox.

C#:
private void filternameTxt_TextChanged(object sender, EventArgs e)
        {
            ClientsTableBindingSource.Filter = string.Format("name LIKE '%{0}%'", filternameTxt_Text);
            DGV.Refresh();
        }
 
Back
Top Bottom