Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a small project I am working on for my kid where he can add pokemon cards to his collection. It's just a C# windows Forms application connected to a SQL server database. I am trying to filter the entries in the database through a dataGridView using text entered in a textbox but it isnt filtering it. The entries just remain unfiltered in the dataGridView. Here is the code for the form.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace JacobsPokedex
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable("Cards");

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString = null;
            connectionString = "Data Source=DESKTOP-NU2DVAO\\SQLEXPRESS;Initial Catalog=Pokedex;Integrated Security=True";

            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                try
                {
                    cnn.Open();
                    using (SqlDataAdapter da = new SqlDataAdapter("select * from Cards", cnn))
                    {
                        da.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
 private void searchByNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (e.KeyChar == (char)13)
             {
                 DataView dv = dt.DefaultView;
                 dv.RowFilter = "pokemon_name LIKE '" + searchByNameTextBox.Text + "%'";
                 dataGridView1.DataSource = dv;
             }
         } 

        private void openAddCardFormButton_Click(object sender, EventArgs e)
        {
            var AddCardForm = new AddCardForm();
            AddCardForm.Show();
        }
    }
}


I also tried this with TextChanged with same results

private void searchByNameTextBox_TextChanged(object sender, EventArgs e)
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter =
            string.Format("pokemon_name LIKE '{0}%' OR pokemon_name LIKE '% {0}%'", searchByNameTextBox.Text);
        }
Posted
Updated 6-Oct-21 20:22pm
v2

1 solution

It works fine for me using a DataView.RowFilter:
/// <summary>
/// A Filter changed - update the DataView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxFilter_TextChanged(object sender, EventArgs e)
    {
    StringBuilder sb = new StringBuilder();
    AppendFilter(sb, "TrackName", tbTrackFilter.Text, "");
    AppendFilter(sb, "Band", tbBandFilter.Text, " AND ");
    AppendFilter(sb, "Album", tbAlbumFilter.Text, " AND ");
    dvFiles.RowFilter = sb.ToString();
    }

/// <summary>
/// Append this clause to the filter statement.
/// </summary>
/// <param name="filter">Filter so far assembled</param>
/// <param name="column">Name of column to add</param>
/// <param name="filterMatchText">Text to look for</param>
/// <param name="append">Prefix string if this clause is valid.</param>
private void AppendFilter(StringBuilder filter, string column, string filterMatchText, string append)
    {
    if (!string.IsNullOrWhiteSpace(filterMatchText))
        {
        // Remove quotes
        filterMatchText = filterMatchText.Replace("'", "''");
        if (filter.Length != 0)
            {
            filter.Append(append);
            }
        filter.AppendFormat(" [{0}] LIKE '%{1}%' ", column, filterMatchText);
        }
    }
So the chances are it's the KeyChar in your code that causes the problem.
Use the debugger, and put a breakpoint on the first line of your handler and step the code through to find out exactly what is happening.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900