C #: changing caret position in DataGridView

In the TextBox, I could use textBox1.SelectionStart = 4

. How do I do this in DataGridView?

edit for clarification: let's assume that a specific cell is already selected and in edit mode. If I press the right arrow key, the caret moves to the desired position across any text in that cell. I want to create a button that does the same.

+1


a source to share


4 answers


You should probably clarify a little. Do you want to change the selected row in the DataGridView or is there a textbox in your DataGridView for which you want to move the cursor?



If you want to change the selected row, try the SelectedIndex property.

+2


a source


private void RadGridView1_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
{
    RadGridView1.CurrentItem = RadGridView1.SelectedItem;
}

      



+1


a source


You need to get the TextBox via the DataGridView EditingControlShowing event.

Store this as a member variable, and when you need to, access the textBox element and set the SelectionStart as you wrote above.

Sort of:

dataGrid.EditingControlShowing += this.dataGrid_EditingControlShowing;

      

and

void dataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if(this.dataGrid.CurrentCell != null && e.Control is TextBox)
            {
                this.currentTextBox = (TextBox)e.Control;
            }
        }

      

0


a source


Whenever the user clicks on your button, the cell in the DataGridView will lose focus, so it will remove the edit box, check the value, and put the formatted value into the cell.

I wonder what are your reasons for the button to move the caret?

0


a source







All Articles