Windows Develop Bookmark and Share   
 index > Windows Forms General > Autocompletion in datagridview
 

Autocompletion in datagridview

Hi,

I'm using autocompletion in my datagridview.
this is the code i'm using:

private AutoCompleteStringCollection codeCompletionSource;
        private AutoCompleteStringCollection omschrijvingCompletionSource;


this.codeCompletionSource = new AutoCompleteStringCollection();
            this.omschrijvingCompletionSource = new AutoCompleteStringCollection();



foreach (DataRow dr in this.database.MagazijnDs.Tables[0].AsEnumerable())
                    {
                        this.codeCompletionSource.Add(dr.ItemArray[1].ToString());
                        this.omschrijvingCompletionSource.Add(dr.ItemArray[2].ToString());
                    }



private void dgvItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvItems.CurrentCellAddress.X == 1)
            {
                TextBox txt = e.Control as TextBox;
                txt.AutoCompleteCustomSource = this.codeCompletionSource;
                txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
            else if (dgvItems.CurrentCellAddress.X == 2)
            {
                TextBox txt = e.Control as TextBox;
                txt.AutoCompleteCustomSource = this.omschrijvingCompletionSource;
                txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
        }

Everything seems to be working fine.
The only problem is that the autocompletion isn't applied to the right columns.
Obviously I want the autocompletion applied to the second and third cell in the datagridviewrows.
The second is working but the third is not. The autocompletion of the third cell does get applied to the fourth, fift, sixt and seventh cell.
Any suggestions?

Best regards,
YMaod
YMaod  Monday, May 04, 2009 1:21 PM
<br/>private void dgvItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {<br/>	//********  FirstColumnName Use your First Column Name here
            if (dgvItems.Columns[this.dgvItems.CurrentCell.ColumnIndex].Name.Equals("FirstColumnName") && e.Control is TextBox)
            {

                     TextBox txt = e.Control as TextBox;
                        if (txt != null)
                        {
                            txt.Multiline = false;
                            txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                            txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
                            txt.AutoCompleteCustomSource = codeCompletionSource;
                        }

            }
		<br/>	//********  SecondColumnName Use your Second Column Name here<br/>
            if (dgvItems.Columns[this.dgvItems.CurrentCell.ColumnIndex].Name.Equals("SecondColumnName") && e.Control is TextBox)
            {

		TextBox txt = e.Control as TextBox;
                        if (txt != null)
                        {
                            txt.Multiline = false;
                            txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                            txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
                            txt.AutoCompleteCustomSource = omschrijvingCompletionSource;;
                        }

            }
                   }

Use Above code. If you have any problem then let me know.
sunDAC  Saturday, May 09, 2009 11:16 AM

Hi YMaod,

I performed a test based on your sample code but didn't reproduce the problem on my side. The autocomplete textbox works for both the second and the third columns in the DataGridView.

Please check whether theomschrijvingCompletionSource in your codecontainselementsor not. To do this, set a break point just after the foreach loop in your code and press F5 to run the application.

In addition, only if you type a value in textbox that has at least a matchinthe autocomplete custom source, the drop down will show with all the matches.

Sincerely,
Linda Liu


Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Linda Liu  Tuesday, May 12, 2009 9:16 AM

You can use google to search for other answers

Custom Search

More Threads

• Treeview
• USB Audio Device?
• I can't save value from combobox
• ComboBox - Add a string to display and associated data
• Cross thread error?
• ToolstripManager
• Using user control on windows forms(C#)
• list view set focus to item
• rectangle event
• Set Font, Color and so on