WesselVB.Net WesselVB.Net - 3 months ago 29
Vb.net Question

Filtering gridview with textbox and button using lists to fill gridview

I am currently busy creating a simple website. Files get copied on to my FTP server. On my web page is a Textbox,button and gridview.

Everything is working fine, when i load my website the gridview is not visible until you have search for something.At the moment i have no search code for my search button, i coded it to load every file into the grid view.

I now want to add some filtering when you click the search button, but i have no clue on how to do it and the Internet only shows how to do it with a database, but i am using New List object.

See my example code below. (This currently add all the files to the gridview)

Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/IIC/"))
Dim dt As New DataTable
dt.Columns.Add("FileName", GetType(String))
dt.Columns.Add("FilePath", GetType(String))
dt.Rows.Clear()
For Each filePath As String In filePaths
dt.Rows.Add(Path.GetFileName(filePath), filePath)
Next

If GridView2.Columns.Count > 2 Then
For x = 2 To GridView2.Columns.Count - 1
GridView2.Columns.RemoveAt(2)
Next
End If


Dim FileNameCol As New BoundField
Dim FilePathCol As New BoundField
FileNameCol.DataField = "FileName"
FileNameCol.HeaderText = "FileName"
FilePathCol.DataField = "FilePath"
FilePathCol.DataField = "FilePath"

GridView2.Columns.Add(FileNameCol)
GridView2.Columns.Add(FilePathCol)

GridView2.DataSource = dt.Select("FileName LIKE '%" & TextBox1.Text & "%'")
GridView2.DataBind()


Any help showing me how to filter the gridview will help me a lot as i haven't really worked with lists before.

Note: The gridview does not use a Database

Designer Code for Gridview2

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" PageSize="10" AllowPaging="true"
EmptyDataText = "No files uploaded" Width="251px">
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

</Columns>



Answer

You can achieve that using a DataTable.

It has a .Select() function in which you can filter specific records inside the datatable (Like the WHERE part in a query but instead from a database, you are using it in a datatable)

Try this:

    Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/IIC/"))
    Dim dt As New DataTable
    dt.Columns.Add("FileName", GetType(String))
    dt.Columns.Add("FilePath", GetType(String))
    dt.Rows.Clear()
    For Each filePath As String In filePaths
        dt.Rows.Add(Path.GetFileName(filePath), filePath)
    Next

    If GridView2.Columns.Count > 1 Then
         For x = 1 To GridView2.Columns.Count - 1
             grdApproval.Columns.RemoveAt(1)
         Next
    End If


    Dim FileNameCol As New BoundField
    Dim FilePathCol As New BoundField
    FileNameCol.DataField = "FileName"
    FileNameCol.HeaderText = "FileName"
    FilePathCol.DataField = "FilePath"
    FilePathCol.HeaderText = "FilePath"

    GridView2.Columns.Add(FileNameCol)
    GridView2.Columns.Add(FilePathCol)

    GridView2.DataSource = dt.Select("FileName LIKE '%" & txtYourSearchBox.Text & "%'")
    GridView2.DataBind()