ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Highlight matching records in Windows Form DataGridView after search result on Button Click using C# and VB.Net<p>Hey <a class="username" rel="satabeach">satabeach</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void Form1_Load(object sender, EventArgs e) { dataGridView1.DataSource = this.PopulateDataGridView(); } private DataTable PopulateDataGridView() { string query = &#34;SELECT CustomerID, Name, Country FROM Customers&#34;; string constr = @&#34;Data Source=.\SQL2005;Initial Catalog=Test;User ID=sa;Password=pass@123&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query, con)) { using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } } } private DataTable FiterDataGridView() { string query = &#34;SELECT CustomerID, Name, Country FROM Customers&#34;; query += &#34; WHERE Country LIKE &#39;%&#39; + @Country + &#39;%&#39;&#34;; query += &#34; OR @Country = &#39;&#39;&#34;; string constr = @&#34;Data Source=.\SQL2005;Initial Catalog=Test;User ID=sa;Password=pass@123&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query, con)) { cmd.Parameters.AddWithValue(&#34;@Country&#34;, txtName.Text.Trim()); using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } } } private void Search_Click(object sender, EventArgs e) { dataGridView1.DataSource = this.PopulateDataGridView(); DataTable filterDt = FiterDataGridView(); for (int i = 0; i &lt; dataGridView1.Rows.Count; i++) { int id = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString().Trim()); string country = dataGridView1.Rows[i].Cells[2].Value.ToString().Trim(); DataRow[] dr = filterDt.Select(&#34;CustomerID=&#34; + id + &#34; AND Country = &#39;&#34; + country + &#34;&#39;&#34;); if (dr.Length &gt; 0) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.White; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load dataGridView1.DataSource = Me.PopulateDataGridView() End Sub Private Function PopulateDataGridView() As DataTable Dim query As String = &#34;SELECT CustomerID, Name, Country FROM Customers&#34; Dim constr As String = &#34;Data Source=.\SQL2005;Initial Catalog=Test;User ID=sa;Password=pass@123&#34; Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(query, con) Using sda As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Function Private Function FiterDataGridView() As DataTable Dim query As String = &#34;SELECT CustomerID, Name, Country FROM Customers&#34; query += &#34; WHERE Country LIKE &#39;%&#39; + @Country + &#39;%&#39;&#34; query += &#34; OR @Country = &#39;&#39;&#34; Dim constr As String = &#34;Data Source=.\SQL2005;Initial Catalog=Test;User ID=sa;Password=pass@123&#34; Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(query, con) cmd.Parameters.AddWithValue(&#34;@Country&#34;, txtName.Text.Trim()) Using sda As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Function Private Sub Search_Click(ByVal sender As Object, ByVal e As EventArgs) dataGridView1.DataSource = Me.PopulateDataGridView() Dim filterDt As DataTable = FiterDataGridView() For i As Integer = 0 To dataGridView1.Rows.Count - 1 Dim id As Integer = Convert.ToInt32(dataGridView1.Rows(i).Cells(0).Value.ToString().Trim()) Dim country As String = dataGridView1.Rows(i).Cells(2).Value.ToString().Trim() Dim dr As DataRow() = filterDt.Select(&#34;CustomerID=&#34; &amp; id &amp; &#34; AND Country = &#39;&#34; &amp; country &amp; &#34;&#39;&#34;) If dr.Length &gt; 0 Then dataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red dataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.White End If Next End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/69rdxfd.gif" alt="" width="451" height="191" /></p>https://www.aspforums.net:443/Threads/205096/Highlight-matching-records-in-Windows-Form-DataGridView-after-search-result-on-Button-Click-using-C-and-VBNet/https://www.aspforums.net:443/Threads/205096/Highlight-matching-records-in-Windows-Form-DataGridView-after-search-result-on-Button-Click-using-C-and-VBNet/Tue, 04 Dec 2018 23:43:08 GMT