-2

I want to search by filter this Data Grid View I have searched so many time for a way to do that but nun of the solutions I found worked with my code ( this is the code I use to load the data grid view )

static public string APP_FOLDER = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tools/Admin Tool/");
public string fileItem = ("Item.txt");

public class itemInterface
{
    public string Code { get; set; }
    public string Item { get; set; }

    public itemInterface(string key, string value)
    {
        Code = key;
        Item = value;
    }
    public itemInterface(string[] value)
    {
        Code = value[0];
        Item = value[1];
    }
}

static public void UpdateItemDataGridView(DataGridView dgv, List<itemInterface> pList)
{
    dgv.ColumnHeadersVisible = false;
    dgv.DataSource = pList;
    dgv.Columns[0].Width = 70;
    dgv.ColumnHeadersVisible = true;
}

private void Form1_Load(object sender, EventArgs e)
{
    List<itemInterface> pList = new List<itemInterface>();
    using (StreamReader sr = new StreamReader(APP_FOLDER + fileItem, Encoding.UTF8, true))
    {
        string s = String.Empty;
        while ((s = sr.ReadLine()) != null)
        {
            itemInterface l = new itemInterface(s.Split('\t'));
            pList.Add(l);
        }
    }
    UpdateItemDataGridView(dataGridView2, pList);
} 
  • Where are you filtering? `pList` isn't being filtered after initialization. Use `linq` or a `for loop` to filter as needed. – SILENT Jun 17 '18 at 07:54
  • sorry but I did not understand I want to filter the plist to fined a specific item by textbox – user2438583 Jun 17 '18 at 08:02
  • Give an example and I'll post an answer. – SILENT Jun 17 '18 at 08:03
  • How about [google](https://stackoverflow.com/questions/26196/filtering-collections-in-c-sharp)? Or do the filtering in a DataView (recommended) – TaW Jun 17 '18 at 08:13
  • it's a data grade view that view a table this table have 2 columns from text file and I want to search that table for a specific item looks like that https://imgur.com/a/AjL0dOw – user2438583 Jun 17 '18 at 08:24
  • Clearly a case for loading into a DataSource and then filteering via a DataView so you won't have to reload from the StreamReader. Look into [dataview.rowfilter](https://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter(v=vs.110).aspx) – TaW Jun 17 '18 at 08:38
  • I did't understand what to do from the link you sent ( can I send you the project source and you help me with making the search by filter work https://1drv.ms/u/s!AiLNiCo4tuKyhuJIeatfbqcL09lzDg – user2438583 Jun 17 '18 at 09:04
  • I found a code but I don't know where to add it pList.FindAll(i => i.Item.ToLower().Contains("Plann".ToLower())); – user2438583 Jun 17 '18 at 09:50

1 Answers1

1

I fined the way to do the search the code is

 List<itemInterface> pList = new List<itemInterface>();
    using (StreamReader sr = new StreamReader(APP_FOLDER + fileItem, Encoding.UTF8, true))
    {
        string s = String.Empty;
        while ((s = sr.ReadLine()) != null)
        {
            itemInterface l = new itemInterface(s.Split('\t'));

            pList.Add(l);

        }
    }

    var pSearch = pList.FindAll(i => i.Item.ToLower().Contains(textBox12.Text.ToLower()));
    UpdateItemDataGridView(dataGridView2, pSearch);