DataGridViewTextBoxEditingControl I don't want text initially sele

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When the user enters a text cell in a datagridview, the text in the cell is
selected, so the first character typed overwrites the text that was selected.
How can I keep the text from being selected so that if the user clicks in the
cell, the text will be ready to be edited, and will not be selected?

-- Mikeq
 
mikeq,

I just did this! Here is what I did:


this.myDataGrid.CellEnter += new
DataGridViewCellEventHandler(myDataGrid_CellEnter);

void myDataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if ((this.myDataGrid.Columns[e.ColumnIndex] is
DataGridViewTextBoxColumn) ||
(this.myDataGrid.Columns[e.ColumnIndex] is
DataGridViewComboBoxColumn))
{
this.myDataGrid.BeginEdit(false);
}
}

Basically, to put that in english, I added an event handler to the data grid
view for the CellEnter event. This event seems to fire any time I enter a
grid, whether by keyboard or mouse.

Then, in the handler for that event, I basically call
dataGridView.BeginEdit(false). The bool (I passed false) is set to true if
you want to select the cell's entire contents. Sounds like you want to pass
in false also. You will note that I am only doing this for text cells and
combobox cells... for all other cells, I am allowing the datagrid to use its
default behavior.

The result of this code is that one click or keystroke, not two, is required
for edit mode, and on top of that the boolean determines whether the edit
mode starts with the text selected or unselected.

Good luck,

-John
 
Thanks Hassiah, that workes great. You would think Microsoft would put
something like that somewhere in thier documentation where it can be found
easily!

Thanks for taking the time to answer, you saved me a lot of time.
--
-- Mikeq


Hassiah said:
mikeq,

I just did this! Here is what I did:


this.myDataGrid.CellEnter += new
DataGridViewCellEventHandler(myDataGrid_CellEnter);

void myDataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if ((this.myDataGrid.Columns[e.ColumnIndex] is
DataGridViewTextBoxColumn) ||
(this.myDataGrid.Columns[e.ColumnIndex] is
DataGridViewComboBoxColumn))
{
this.myDataGrid.BeginEdit(false);
}
}

Basically, to put that in english, I added an event handler to the data grid
view for the CellEnter event. This event seems to fire any time I enter a
grid, whether by keyboard or mouse.

Then, in the handler for that event, I basically call
dataGridView.BeginEdit(false). The bool (I passed false) is set to true if
you want to select the cell's entire contents. Sounds like you want to pass
in false also. You will note that I am only doing this for text cells and
combobox cells... for all other cells, I am allowing the datagrid to use its
default behavior.

The result of this code is that one click or keystroke, not two, is required
for edit mode, and on top of that the boolean determines whether the edit
mode starts with the text selected or unselected.

Good luck,

-John

mikeq said:
When the user enters a text cell in a datagridview, the text in the cell is
selected, so the first character typed overwrites the text that was selected.
How can I keep the text from being selected so that if the user clicks in the
cell, the text will be ready to be edited, and will not be selected?

-- Mikeq
 
Back
Top