469,303 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,303 developers. It's quick & easy.

AutoCompleteStringCollection for text box in DataGridView 2.0

I have been searching for a way to have autocomplete in a DataGridTextBox
object. There does not appear to be an obvious way to do this like you would
for a TextBox outside of a grid using the AutoCompleteCustomSource. Any
thoughts on how I can add this collection? :)
Jan 6 '06 #1
3 21081
It's actually pretty easy (although as far as I know no one has posted
this code yet). Someone please let me know if this is the wrong way of
doing it:

Just listen for the EditingControlShowing event of the DataGridView.
This will give you a chance to mess with the cell before it allows the
user to edit.

In this case, I did the following:

private void m_Grid_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl te =
(DataGridViewTextBoxEditingControl)e.Control;
te.AutoCompleteMode = AutoCompleteMode.Append;
te.AutoCompleteSource = AutoCompleteSource.CustomSource;
te.AutoCompleteCustomSource.AddRange(new string[] {"one", "two",
"three"});
}
}

This works because DataGridViewTextBoxEditingControl is just a
subclassed TextBox.

Hope this helps.

Jan 9 '06 #2
Also, thank you for the great post! It put me in the right direction!

"B Loggins" wrote:
It's actually pretty easy (although as far as I know no one has posted
this code yet). Someone please let me know if this is the wrong way of
doing it:

Just listen for the EditingControlShowing event of the DataGridView.
This will give you a chance to mess with the cell before it allows the
user to edit.

In this case, I did the following:

private void m_Grid_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl te =
(DataGridViewTextBoxEditingControl)e.Control;
te.AutoCompleteMode = AutoCompleteMode.Append;
te.AutoCompleteSource = AutoCompleteSource.CustomSource;
te.AutoCompleteCustomSource.AddRange(new string[] {"one", "two",
"three"});
}
}

This works because DataGridViewTextBoxEditingControl is just a
subclassed TextBox.

Hope this helps.

Jan 9 '06 #3
The only change I made was to be specific about the autocomplete information
for each column.
private void dataGridView_fish_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl te =
(DataGridViewTextBoxEditingControl)e.Control;
te.AutoCompleteMode = AutoCompleteMode.Append;
te.AutoCompleteSource = AutoCompleteSource.CustomSource;

DataGridView dgv = (DataGridView)sender; //reference to the
original grid
int col = dgv.CurrentCell.ColumnIndex; //columnindex if the
current cell

switch (col) //check for each col (column number) and set
the autocomplete for it
{
case 0: // column 0
te.AutoCompleteCustomSource.AddRange(new string[]
{"abc", "def", ghi"});
break;
case 1:
te.AutoCompleteCustomSource.AddRange(new string[]
{"jkl", "mno", "pqr"});
break;

default:
break;
}
Jan 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mark Carew | last post: by
2 posts views Thread by jack | last post: by
reply views Thread by Pieter Coucke | last post: by
2 posts views Thread by Scotty | last post: by
4 posts views Thread by gregarican | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
3 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.