Windows Develop Bookmark and Share   
 index > Windows Forms General > Datagridviews and autocomplete
 

Datagridviews and autocomplete

Is there more than one way of adding values to an autocomplete custom collection of a datagridview? I'm familiar with the technique that uses the EditingControlShowing event, but I have a datagridview where I only need to set the autocomplete values once at the point at which the form is displayed, and not every time this event occurs.

I think what I need to know is how to access the "control within a control" that the e.Control of the EditingControlShowing event arguments represents, when I don't have an 'e' to play with.

Hope this makes sense.
P.S.  Monday, August 03, 2009 5:31 PM
Hi P.S.,

You'd better still handle EditingControlShowing event to do the work. But you need to set that only once. Look at the following code.
public partial class Form1 : Form
{
private bool autoCompleteSet;

public Form1()
{
InitializeComponent();
autoCompleteSet = false;
dataGridView1.Columns.Add("Column1", "Column1");
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (!autoCompleteSet)
{
TextBox txtBox = e.Control as TextBox;
if (txtBox != null)
{
txtBox.AutoCompleteMode = AutoCompleteMode.Suggest;
txtBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtBox.AutoCompleteCustomSource.Add("abcde");
txtBox.AutoCompleteCustomSource.Add("abdce");
txtBox.AutoCompleteCustomSource.Add("bcade");
autoCompleteSet = true;
}
}
}
}

In the form, there is an autoCompleteSet member, it is set to false at first. When you edit the cell, autocomplete items will be added and autoCompleteSet member will be set to true. So the next time, it won't add these items again. I think this can be a good solution for you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, August 05, 2009 7:42 AM
Dont you have an 'e' to play with by using CellEndEdit or something?
Kenneth
Kenneth Haugland  Monday, August 03, 2009 10:02 PM
Can you explain how this would enable me to set the autocomplete values once only?
P.S.  Monday, August 03, 2009 10:28 PM
Hi P.S.,

You'd better still handle EditingControlShowing event to do the work. But you need to set that only once. Look at the following code.
public partial class Form1 : Form
{
private bool autoCompleteSet;

public Form1()
{
InitializeComponent();
autoCompleteSet = false;
dataGridView1.Columns.Add("Column1", "Column1");
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (!autoCompleteSet)
{
TextBox txtBox = e.Control as TextBox;
if (txtBox != null)
{
txtBox.AutoCompleteMode = AutoCompleteMode.Suggest;
txtBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtBox.AutoCompleteCustomSource.Add("abcde");
txtBox.AutoCompleteCustomSource.Add("abdce");
txtBox.AutoCompleteCustomSource.Add("bcade");
autoCompleteSet = true;
}
}
}
}

In the form, there is an autoCompleteSet member, it is set to false at first. When you edit the cell, autocomplete items will be added and autoCompleteSet member will be set to true. So the next time, it won't add these items again. I think this can be a good solution for you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, August 05, 2009 7:42 AM

You can use google to search for other answers

Custom Search

More Threads

• What is a ComboBox Items Data Type?
• tabpage control navigation is not refreshing the panels quick enough.
• I need help about drawing rectangle on htmldocument
• Image merging.
• How to get DOS 8.3 Filename and Folder names using Visual Basic .NET
• Is there a way to create something that is similar to masterpages in a Windows App?
• Finding Image/Link Properties
• IAzRolesLib from .NET 2.0
• problem saving user app settings to disk.
• Apply TextRenderingHint to normal font w/ Graphics class and Paint Event?