Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView: enter cell without it being entirely selected

Status
Not open for further replies.

Shimmy613

Programmer
Feb 14, 2011
39
0
0
US
I have a DataGridView with a button that I created that adds a new row to the DataGridView - that works fine. The problem is that when the user clicks that button, the entire cell is selected. In order to allow typing into that cell, the user has to first click in the cell. I'm told this is annoying.

Let me interrupt myself right here and say: If anyone has an easy way to accomplish this, please suggest it. Because what I'm about to describe (i.e. what I am currently trying) seems far too complicated to achieve what seems to be a simple goal...

So... I added the following line to the end of my "btnAddRow_Click" event:
Code:
dgvAnnouncements.Rows[dgvAnnouncements.RowCount - 1].Cells[0].Selected = true;
I also hooked the following two methods as follows:
Code:
        private void dgvAnnouncements_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
           dgvAnnouncements.BeginEdit(false);
        }

        private void dgvAnnouncements_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            dgvAnnouncements.EndEdit();
        }
That actually all seems to work well. Until something happens - like they do something to get the DataGridView to be redrawn (I think). Then this exception is thrown:
[tt]Operation is not valid due to the current state of the object[/tt]

I tried to also to call
Code:
dgvAnnouncements.EndEdit();
when the user leaves the tabpage (i.e. the DataGridView is being hidden) - but still got the same error.

Can anyone help me, please?

Thanks!!!
 
I just realized I forgot an important detail. What I do want to happen is to have the blinking carat/cursor in the cell as soon as the user adds the new row, not have that entire cell selected (i.e. blue).
Thanks!
 
Code:
textBox1.Text = "Stuff and such";
textBox1.Focus();
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;

You'll have to add check to ensure there are rows in the datagrid before trying to interact with it.

HTH,
Lodlaiden

A lack of experience doesn't prevent you from doing a good job.
 
Lodlaiden, would I do that in the [tt]dgvAnnouncements_CellEnter[/tt] event?
Do I need to BeginEdit altogether?

Thanks so much!...
 
You shouldn't need the begin/end edit items.
use the "e" parameter to get the row and the column that is being modified and if it's one of the ones that you know has a text field in it then set the selection rows.

usually you do checks to ensure that the column is one you can about, and not the column with the button(s) in it.



A lack of experience doesn't prevent you from doing a good job.
 
Thank you for your replies. But what I still don't understand is when I do this:
Code:
        private void dgvAnnouncements_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = dgvAnnouncements.CurrentCell;
        }
None of these:
.Focus
.SelectionStart
.SelectionEnd
work for my "[tt]cell[/tt]" object; it just doesn't have those properties. It's not a textbox - it's a DataGridViewCell.

???

Thanks!
 
Then this
looked promising - so I tried this:
Code:
        private void dgvAnnouncements_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvAnnouncements.CurrentCell.IsInEditMode && dgvAnnouncements.EditingControl.GetType() == typeof(DataGridViewTextBoxEditingControl))
            {
                SendKeys.Send("{RIGHT}");
            }

        }
...but EditingControl was always null...
 
use the [red]e[/red] parameter to tell you which cell is being edited.
you need to cast the cell to the appropriate type before you can do things like modify the cursor position. A generic DataGridCell may be a combo box, a checkbox, or a button. None of those have text areas to select.

Lodlaiden

A lack of experience doesn't prevent you from doing a good job.
 
Lodlaiden, I'm sure I'm missing something. Please take a look at what I tried and the compiler errors that I received:
Code:
        private void dgvAnnouncements_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = dgvAnnouncements[e.ColumnIndex, e.RowIndex];
            if (cell != null && cell.GetType() == typeof(DataGridViewTextBoxCell))
            {
                DataGridViewTextBoxCell textBoxCell = ((DataGridViewTextBoxCell)cell);

                [b]//Compiler error: 'System.Windows.Forms.DataGridViewTextBoxCell' does not contain a definition for 'SelectionStart' and no extension method 'SelectionStart' accepting a first argument of type 'System.Windows.Forms.DataGridViewTextBoxCell' could be found[/b]
                textBoxCell.SelectionStart = 0;

                [b]//Compiler error: Cannot convert type 'System.Windows.Forms.DataGridViewCell' to 'System.Windows.Forms.TextBox'[/b]
                TextBox txtCurrent = ((TextBox)cell);
            }
        }
So sorry - could you please clarify?

Thanks!!!
 
This should work, but something is reselecting the text after

Code:
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType()== typeof(DataGridViewTextBoxEditingControl)) // "System.Windows.Forms.DataGridViewTextBoxEditingControl")
            {
                DataGridViewTextBoxEditingControl c = (DataGridViewTextBoxEditingControl)e.Control;
                c.DeselectAll();
            }
        }

A lack of experience doesn't prevent you from doing a good job.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top