Solved

EditingControlShowing - How to pre-select text when user moves to new cell

Posted on 2008-10-16
1
1,028 Views
Last Modified: 2013-12-17
Here's what I want to do:

In a Datagridview, if the user is editing a cell and has the cursor in, say, position 10, and then moves down or up to an adjacent cell, I want to the new cell to be in edit mode and have the cursor in position 10 with only that position selected (or none selected.) - similar to what you see in any text editor as you move up or down.

However, whenever I move to the next cell, it does go into edit mode, but the whole cell is selected even though I selected just that one position.
private void dgrSource_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            Control ctl = e.Control;
            DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)ctl;
                              
			if (lastPos > 0 && lastPos < dText.Text.Length)  //lastPos was previously captured and is correct
            {
                dText.SelectionStart = lastPos;
                dText.SelectionLength = 1;
            }
        }

Open in new window

0
Comment
Question by:stevelucy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
stevelucy earned 0 total points
ID: 22737304
I solved this by capturing the DataGridViewTextBoxEditingControl variable in the EditingControlShowing event, and performing the Select method in the CellEnter event.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question