AutoComplete doen't work when datagridview created dinamicly

Hello.
1. I has created a datagridview with some textbox cells dinamicly
2. I'm trying to add autocomplete text to cell (it doen't work when datagridview created dinamicly)

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            (e.Control as DataGridViewTextBoxEditingControl).TextChanged -= cell_TextChanged;
            (e.Control as DataGridViewTextBoxEditingControl).TextChanged += cell_TextChanged;
        }

        void cell_TextChanged(object sender, EventArgs e)
        {
            AutoCompleteStringCollection acCollection = new AutoCompleteStringCollection();
            acCollection.Add("1122");
            acCollection.Add("1133");
            acCollection.Add("1144");
            acCollection.Add("1222");
            acCollection.Add("1333");
            acCollection.Add("1444");

            TextBox tb = sender as TextBox;
            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tb.AutoCompleteCustomSource = acCollection;
            tb.AutoCompleteMode = AutoCompleteMode.Suggest;   
        }

Open in new window


Why autocomplete list does not showing?
I'm sorry for my bad english....
Dmitriy GalankinDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Works for me:

Form1.cs -
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

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

        private void OnLoad(object sender, EventArgs e)
        {
            dataGridView1.DataSource = (from i in Enumerable.Range(0, 20) select new { ID = i, Name = $"Name{i}", Birthdate = DateTime.Now.AddDays(-(9 * i)), IsWorking = i % 2 == 0 }).ToList();
        }

        private void OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (sender is DataGridView)
            {
                var grid = sender as DataGridView;
                if (grid.Rows.Count > 0)
                {
                    var column = grid.Columns[grid.CurrentCell.ColumnIndex] as DataGridViewColumn;
                    if (column != null && column.DataPropertyName.Equals("Name"))
                    {
                        var tb = e.Control as TextBox;
                        if (tb != null)
                        {
                            var collection = new AutoCompleteStringCollection();
                            collection.AddRange((from row in grid.Rows.Cast<DataGridViewRow>()
                                                 select row.Cells[column.Index].Value.ToString()).ToArray());
                            tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                            tb.AutoCompleteCustomSource = collection;
                            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
                        }
                    }
                }
            }
        }

        private void OnCellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sender is DataGridView)
            {
                var grid = sender as DataGridView;
                grid.CurrentCell.ReadOnly = false;
            }
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29086059
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.Size = new System.Drawing.Size(584, 261);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.OnCellClick);
            this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.OnEditingControlShowing);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(584, 261);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dmitriy GalankinDeveloperAuthor Commented:
Dear Saige, Thank you.

Your collection is formed in OnEditingControlShowing event.
My collection will consist from Russian postal address data (it's very big data)

I need to form a collection in cell_TextChanged event.

So i can form this data and my autoCompleteStringCollection isn't empty, but panel with autocomplete list doesn't show...
0
Dmitriy GalankinDeveloperAuthor Commented:
I has found solution!

The WrapMode propertie of cell must be DataGridViewTriState.False
Then AutoCompleteList will showing.
0
Dmitriy GalankinDeveloperAuthor Commented:
The problem was solved
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.