Net-informations.com
SiteMap  | About    

DataGridView Autocomplete TextBox

Displaying data in a tabular format is a task you are likely to perform regularly. The C# DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its display and behavior.

DataGridView Autocomplete TextBox in C#

DataGridView

C# AutoComplete in DataGridView

In C# you can display DataGridView in Bound mode, unbound mode and Virtual mode . Bound mode is suitable for managing data using automatic interaction with the data store. The common use of the DataGridView control is binding to a table in a database. The data can be displayed in the DatagridView using TextBox, ComboBox, CheckBox etc. While you display the data on TextBox, you can make your TextBoxes as Autocomplete TextBox.

AutoComplete in DataGridView

C# Autocomplete TextBox

The TextBox properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data source.

The following C# program shows how to create an AutoComplete Textbox inside a DataGridView.




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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string titleText = dataGridView1.Columns[1].HeaderText;
            if (titleText.Equals("Product Name"))
            {
                TextBox autoText = e.Control as TextBox;
                if (autoText != null)
                {
                    autoText.AutoCompleteMode = AutoCompleteMode.Suggest;
                    autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
                    addItems(DataCollection);
                    autoText.AutoCompleteCustomSource = DataCollection;       
                }
            }
         }

        public void addItems(AutoCompleteStringCollection col)
        {
            col.Add("Product 1");
            col.Add("Product 2");
            col.Add("Product 3");
            col.Add("Product 4");
            col.Add("Product 5");
            col.Add("Product 6");
        }

    }
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.