C# Snippet Tutorial - Modify a Cell's Selected Text

Skill

C# Snippet Tutorial - Modify a Cell's Selected Text

Posted in:

In this short tutorial, I'm going to show you how to change the text selection in a DataGridViewCell. There's lots of reasons why someone would want to do this, like if someone enters something incorrectly, maybe you want to select all the text so they can enter something else.

The easiest want to select all the text in a cell is call BeginEdit on the DataGridView. The BeginEdit function takes a parameter that tells it whether or not to select all the text in the cell.

_myDataGrid.BeginEdit(true);

This is great, except when you want to select text while you're already in edit mode. This can be accomplished by actually getting a hold of the edit control and working directly with it. The DataGridView exposes the control through the property, EditingControl. Depending on what type of column the cell is in, the EditingControl could be many different types. For this tutorial, I'm going to assume we're using the standard DataGridViewTextBoxColumn, which will give us a DataGridViewTextBoxEditingControl. Boy, .NET has some long names.

DataGridViewTextBoxEditingControl editControl =
    (DataGridViewTextBoxEditingControl)_myDataGrid.EditingControl;

If the DataGridView doesn't have a cell in edit mode, the EditingControl will be null, so you might want to check for that before casting it. Now to set the text selection, we can do it like we would any regular TextBox.

DataGridViewTextBoxEditingControl editControl =
    (DataGridViewTextBoxEditingControl)_myDataGrid.EditingControl;

//selects to the first two characters
editControl.SelectionStart = 0;
editControl.SelectionLength = 2;

//selects all the text
editControl.SelectionStart = 0;
editControl.SelectionLength = editControl.Text.Length;

Just like with a TextBox, you can set the SelectionStart and SelectionLength properties to change the cell's selected text. I think that just about does it for this snippet tutorial. If you've got any comments or questions, just post them below.

Dudeman
07/06/2008 - 10:10

so you are getting an editingcontrol from _myDataGrid, which i assume is a cell. But a grid can have many cells (columns, rows) how do you know which cell do you have. I didn't see you specify the row or column any where in this sample. I am confused.

reply

The Reddest
07/06/2008 - 15:41

_myDataGrid is the actual DataGridView. The EditingControl is the control located inside the currently selected cell. I'm relying on the user to select the cell, instead of me explicitly setting it. You can change the currently selected cell by using the CurrentCell property on the DataGridView if you want to modify the text in a cell that is not already selected.

reply

joven
07/14/2009 - 00:37

i have a problem in getting DataGridViewTextBoxEditingControl into not null. coz i always get a null values.. here is my codes

dgv.CurrentCell = dgv[x, y];

dgv.BeginEdit(true);
DataGridViewTextBoxEditingControl editControl = (DataGridViewTextBoxEditingControl)dgv.EditingControl;

//Heres come the problem editControl is null
//how can make the editControl not null? pls help me..
editControl.Select(3, 1);
editControl.ScrollToCaret();
editControl.Focus();

reply

vikas200002000
08/31/2009 - 03:44

I Have got great help from your code. I have created datagrid bound with MySQL database using your code. Dear I am getting bif problem in updating or modifying cell value. I have done that on button click event

private void sBut_Click(object sender, EventArgs e)
{
bSource.EndEdit();
dAdapter.Update(dTable);
}

I have also tried rowsremoved and CellValueChanged event of DataGrid View, but failed to implement. Please help me.

reply

dblai
09/21/2009 - 10:54

Thanks a ton!! Huge help! I was stuck on a similar problem like this for a while but could not figure out how to grab the selected text in a datagridview, only in a datagrid. Really appreciate the help!!!

reply

Michael
10/29/2009 - 07:33

when I leave the selected cell How can I select it again

reply

The Reddest
10/29/2009 - 08:34

Set the CurrentCell property on the DataGridView to the cell you want selected.

reply

dominique
11/05/2009 - 09:17

Very good. Thanks you very much.

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors