Skip to main content Skip to footer

How to Create a .NET AI Filter Prompt with FlexGrid for WinForms

Quick Start Guide
What You Will Need

Visual Studio 2022

Gemini AI

Controls Referenced

FlexGrid for WinForms

Tutorial Concept Explore implementing a Gemini-powered .NET AI filter prompt that works with FlexGrid for WinForms. The AI prompt allows the end-user to query and filter the datagrid using natural language.

Introducing the Gemini AI Grid Assistant

Starting with the 2025 v1 release, we've introduced an exciting new sample for FlexGrid in WinForms that showcases the power of integrating AI with modern .NET UI controls. This sample demonstrates how you can leverage natural language prompts to generate structured, dynamic data and display it seamlessly within a FlexGrid.

Ready to check it out? Download ComponentOne Today!

The AI model behind this functionality is Gemini AI, which is free to use and provides a smart, intuitive interface for users who want to interact naturally with their data.

Download the sample or fork on GitHub.

In this blog, we will discuss:

First, let's explore how the Gemini AI Grid Assistant sample enhances the user experience by making data filtering easier and more intelligent.

Starting off easy, the AI filter works with natural language prompts like "show products that start with C" rather than a syntax-explicit expression like SQL: SELECT * FROM Products WHERE ProductName LIKE 'C%';

Gemini AI Filter

That's cool, but let's try getting more intelligence from this .NET AI filter. A prompt like "display products shipped from Europe" will automatically cross-reference the Country field to a list of European countries without requiring you to provide this extra step or include a 'Continent' or 'Region' field.

AI Filter Europe

Still not impressed? We can also elicit complex responses from this, like "list all products that contain Spanish words in their names". We're unsure of the use case, the accuracy, or how much extra processing is required, but this shows the potential of the smart .NET AI filter.

AI Filter Spanish

The Gemini AI Grid Assistant handles more than filtering. It also supports the following prompt types:

  • Comparisons - "Which product is cheaper?"
  • Aggregations - "Sum the total price."
  • Extremes - "Show the product with the highest revenue."

In some cases, the FlexGrid is not filtered, but the answer you're looking for simply displays in the output textbox.

Gemini AI Prompt

You can download and build the sample yourself with a Gemini API key. Jump to Step #2 below to learn more about generating the key.

Why Use AI-Powered Filters?

Traditional data filters often come with rigid requirements: users must know the exact column names, data types, and filtering syntax. This can be a barrier, especially for casual users or those unfamiliar with the data structure.

With AI-powered filters, users can type natural, phrase-based queries like in a web search. The AI understands the intent and translates it into precise data filters with no need to memorize field names or formats.

How AI Makes Filtering Smarter

Consider this simple example:

"Show products shipped from Europe"

A traditional approach would require:

  1. Identifying all European countries
  2. Filtering the dataset manually using each one

With AI, this complexity is handled automatically. The model interprets the phrase, generates the relevant list of countries behind the scenes, and applies the correct filter, saving you time and reducing errors. The result is a more natural and productive way to work with data.

How to Create a .NET AI Filter

Next, we've listed the basic steps taken to achieve the .NET AI Filter assistant sample with FlexGrid.

Step 1 - Create Data-Bound Grid Form

Create a WinForms app form that includes, at the very least, a FlexGrid, a TextBox, and a Button to send the prompt. Our sample also includes buttons to reset the filter and clear the text, as well as a textbox to display output messages from Gemini AI Studio.

Step 2 - Create a Gemini API Key

The sample requires you to generate your own Gemini API key. It's free to create one (with limitations), but for final production, you'll want to add encryption or secure it in a config file or environment variable for security.

To generate a Gemini API key, follow the steps below:

  1. Sign in to Gemini AI Studio.
  2. Click "Get API Keys".
  3. Click "Create API key".
  4. Select the Gemini API project and click "Create API key in existing project".
  5. Copy the key (it will only be shown once).

Step 3 - Connect to Gemini API

Below, we use our Gemini API key and HttpClient to make a request.

/// <summary>
/// Sends a prompt and JSON data to the Gemini API and returns the response.
/// </summary>
public async Task<string> GetResponseAsync(string prompt, string jsonData)
{
    string apiKey = EnvironmentHelper.GetApiKey();
    string apiUrl = $"{BaseUrl}?key={apiKey}";

    var content = BuildRequestContent(prompt, jsonData);
    var response = await _httpClient.PostAsync(apiUrl, content);
    
    if (!response.IsSuccessStatusCode)
    {
        // Use the factory method to create the appropriate exception
        throw GeminiApiException.CreateFromStatusCode((int)response.StatusCode, response.ReasonPhrase);
    }

    return await response.Content.ReadAsStringAsync();
}

The sample works by using the prompt written by the user, combining it with logical instructions, and sending the combined content request for the AI, such as this:

Prompt: {prompt}
Data: {jsonData}
Instructions: ...

The request content also includes the JSON data and instructions, such as:

  • If the prompt is related to the data, respond based on the type of analysis required:
    • a. If it's a comparison, respond in this format:
      • Response: <brief explanation (5–20 words)>
      • Data: <valid JSON array>
    • b. If it's an aggregation (e.g., totals, averages, summaries), respond in this format:
      • Response: <brief explanation with the answer (5–20 words)>
  • If the prompt is not related to the data, respond only with:
    • Response: <brief explanation (5–20 words)>

The response from Gemini includes a brief explanation along with metadata and the filtered data (if applicable).

Download the Full Gemini AI Grid Assistant Sample

Fork the sample on GitHub, download it directly, or if you've already installed the ComponentOne WinForms controls, launch the Sample Explorer and search for "Gemini."

Gemini AI FlexGrid

Ready to try it out? Download ComponentOne Today!

In the sample code, you will find a few Gemini helper classes. The GeminiHelper and GeminiUtils classes abstract all the AI-related logic, making the integration clean and modular. By separating concerns this way, the UI remains lightweight. These classes contribute as detailed below:

  • GeminiHelper.cs:
    • Handles communication with the Gemini API
    • Takes user prompts and grid data (as JSON), then constructs a request to Gemini
    • Returns the AI-generated response (e.g., modified data, insights) for display or further processing
  • GeminiUtils.cs:
    • Converts DataTable ⇄ JSON (for serialization/deserialization)
      • Formats grid content for better AI context
      • Sanitizes or validates responses before pushing to the UI
      • Contains helper methods

Conclusion

The goal of this sample was to enhance the traditional FlexGrid filtering experience by allowing users to type plain-language queries, which are interpreted and applied as filters. We chose Gemini AI, but the same concepts can be applied to other language models, such as ChatGPT or Copilot.

There are some additional considerations you should make to implement this sample code in your application. The accuracy of Gemini's response depends heavily on how the data and prompts are structured. During development, we added helper logic to ensure grid data was clean and compact when sent, but these instructions can be expanded upon. The sample also works by sending the data to Gemini. This gives full power and enables complex prompts like summing totals, applying queries, and delivering filters that could not possibly be translated to a simple filter definition.

We plan to provide a more limited C# .NET AI Filter sample that does not require the full data set on the server, so stay tuned, and let us know what you would like to see from ComponentOne controls with AI integration.

comments powered by Disqus