How do I search data in a gridview (data isn't from database) ASP.NET

Tinsae Telu 21 Reputation points
2022-02-05T22:50:23.813+00:00

Most search features of grid view in ASP.NET shows how to search data from a data list that is connected to a database. But my data in my gridview is list of name of books from a folder. It uploads files from folders and save them on visual studio folder, not database. Now I want to search data from the gridview but all solutions are talking about how to search from database. Your help contributes a lot for my web app. I am almost on the last stage to launch it. Thanks.

What I have tried:

I have tried to use datatable javascript (DataTable is a plug-in for the jQuery Javascript library). But since I have another gridview in the same page, the search and other functionalities don't work on the second gridview (That is also another problem to be addressed. Solution is appreciated). That is why I want to use another searching method.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,270 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,276 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,516 Reputation points
    2022-02-07T01:40:22.233+00:00

    You can create a DataTable and fill it with data that is from anywhere. Then you can bind that to the GridView. Then you can use various possible solutions for searching; either LInQ or the DataTable.Select Method should work. If it is just one table (you can have multiple DataTables in a DataSet and define relationships among them) then you can use a generic collection such as the List<T> Class.


3 additional answers

Sort by: Most helpful
  1. Sreeju Nair 11,616 Reputation points
    2022-02-06T07:27:53.373+00:00

    Based on your question, I understand you want to apply search features for a grid, where you loaded items from your file system and based on your input, you are already using DataTable plugin to display the data. Generally, you can have multiple independant instances of these instances in a single page. I found an article that states how you can use multiple datatables in a single page.

    https://datatables.net/examples/basic_init/multiple_tables.html

    Though this will work, still there are more structured way you can achive this. You can use controls that is specifically designed for .Net applications that helps you bind data and enable standard features. Refer the following sample that shows how to use Syncfusion DataGrid and do searching in the data.

    https://ej2.syncfusion.com/aspnetcore/Grid/Search#/bootstrap5


  2. Rijwan Ansari 746 Reputation points MVP
    2022-02-06T15:16:21.187+00:00

    Hi @Tinsae Telu

    Alternatively, You can add text box with search click button. You can search in folder and reload the grid view.

    Sample:

    	private void btnSearch_Click(object sender, EventArgs e)  
            {  
                List<FileList> oLst = new List<FileList>(); // list based on your grid  
      
                string partialName = txtSearch.Text;//search Field  
      
                DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"Directory Location");  
                FileSystemInfo[] filesAndDirs = hdDirectoryInWhichToSearch.GetFileSystemInfos("*" + partialName + "*");  
      
                foreach (FileSystemInfo foundFile in filesAndDirs)  
                {  
                    string fullName = foundFile.FullName;  
    				 oLst.Add(new FileList // create list based on your gird  
                        {  
                            Name = fileInfo.Name,  
                            Type = "File", //   
                            location = fileInfo.FullName,  
                            Size = Format.ByteSize(fileInfo.Length)  
                        });  
                }  
                dataGridView1.DataSource = oLst;  
            }  
    

  3. Sreeju Nair 11,616 Reputation points
    2022-02-07T05:07:19.147+00:00

    Please make sure your are using the datatable plugins from the below site.

    https://datatables.net/examples/basic_init/multiple_tables.html

    Then you add a class to your table like following.

    <table id="" class="display" style="width:100%">

    Now in the .Net code, you do the following.

    171792-image.png

    I just replaced your datatable initiation script with the above one.

    Also make sure , you have the link to the datatable js file.

    https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js and jquery

    0 comments No comments