Refer the below sample code for your reference and implement it in your code as per your logic.
The CSV File
HTML
<form id="form1" runat="server">
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnImport" runat="server" Text="Search" OnClick="Search" />
<hr />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
C#
protected void Search(object sender, EventArgs e)
{
string searchValue = txtSearch.Text.Trim();
// Set File path.
string csvPath = Server.MapPath("~/Files/Sample.csv");
//Create a DataTable.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
//Read the contents of CSV file.
string csvData = File.ReadAllText(csvPath);
//if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
//{
//Execute a loop over the rows.
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
bool searchValueExist = false;
//Execute a loop over the columns.
foreach (string cell in row.Split(','))
{
bool valueExist = cell.ToUpper().Contains(searchValue.ToUpper());
if (valueExist)
{
searchValueExist = true;
break;
}
}
if (searchValueExist)
{
dt.Rows.Add();
int i = 0;
//Execute a loop over the columns.
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
}
//}
//Bind the DataTable.
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim searchValue As String = txtSearch.Text.Trim()
Dim csvPath As String = Server.MapPath("~/Files/Sample.csv")
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
Dim csvData As String = File.ReadAllText(csvPath)
For Each row As String In csvData.Split(vbLf)
If Not String.IsNullOrEmpty(row) Then
Dim searchValueExist As Boolean = False
For Each cell As String In row.Split(","c)
Dim valueExist As Boolean = cell.ToUpper().Contains(searchValue.ToUpper())
If valueExist Then
searchValueExist = True
Exit For
End If
Next
If searchValueExist Then
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As String In row.Split(","c)
dt.Rows(dt.Rows.Count - 1)(i) = cell
i += 1
Next
End If
End If
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot