Mystic Lin
Mystic Lin

Reputation: 375

How to disable per cell tab stop of DataGridView in C#?

How to disable per cell tab stop of DataGridView in C# ?

If users focus on DataGridView and press 'Tab', I hope the next control would be focused, not focusing on next cell of DataGridView.

How can I do that?

Upvotes: 4

Views: 7309

Answers (4)

Jwdsoft
Jwdsoft

Reputation: 461

Here is how I did it. It took me a lot of trial an error to get to the right code because it was a bit tricky. make a class that inherits DataGridView in order to make a custom DataGridView compile the project and MyDataGridView will appear in your toolbox. it new functionality is to bypass the readonly cells and you use either ENTER or TAB to navigate between cells

public class MyDataGridView : DataGridView
{

    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        if (keyData == Keys.Tab || keyData == Keys.Enter)
        {
            int index = this.CurrentCell.ColumnIndex;
            if (index < this.ColumnCount - 1)
            {
                if (this.CurrentRow.Cells[index + 1].ReadOnly)
                    SendKeys.Send("{TAB}");
            }
                return this.ProcessTabKey(keyData);
        }else if(keyData == (Keys.Enter | Keys.Shift) || keyData == (Keys.Tab | Keys.Shift))
        {
            int index = this.CurrentCell.ColumnIndex;
            if (index > 0)
            {
                if (this.CurrentRow.Cells[index - 1].ReadOnly)
                    SendKeys.Send("+{TAB}");
            }
            return this.ProcessLeftKey(keyData);
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Upvotes: 0

GuidoG
GuidoG

Reputation: 12014

This will make the next control get focus:

private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Tab)
   {
       DataGridView1.Enabled = false;
       DataGridView1.GetNextControl(DataGridView1, true).Focus();
       DataGridView1.Enabled = true;
       e.Handled = true;
   }
}

When using the KeyUp the datagridview still moves one cell further before giving up focus. If you want to undo that you can add this line of code:

DataGridView1.CurrentCell = DataGridView1.Rows[DataGridView1.CurrentCell.RowIndex].Cells[DataGridView1.CurrentCell.ColumnIndex - 1];
DataGridView1.Enabled = false;
DataGridView.GetNextControl(DataGridView1, true).Focus();
DataGridView1.Enabled = true;

Upvotes: -1

JoeMilo
JoeMilo

Reputation: 133

Set the DataGridView.StandardTab property to true

Here is the description for the property:

"Indicates whether the TAB key moves the focus to the next control in the tab order rather than moving focus to the next cell in the control."

In VS2012 .NET 4 this was false by default (didn't see default state in docs) after adding a new datagridview to a form.

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.standardtab(v=vs.110).aspx

I'd recommend when searching for a solution to a problem, type the object and desired functionality into google followed by a space and then the text msdn. This solves about 40% of my dilemmas. If that doesn't work replace msdn with stackoverflow. This solves another 50%. The last 10% are the result of taking a break and coming back to it.

Upvotes: 9

Mohit S
Mohit S

Reputation: 14044

Make AllowUserToAddRows = false and then

private void ToNextControl(bool Forward)
{
    Control c = Parent.GetNextControl(this, Forward);         
    while (c != null && !c.TabStop) // get next control that is a tabstop
        c = Parent.GetNextControl(c, Forward);
    if (c != null)
    {
        //c.Select(); // Not needed for it to work
        c.Focus(); // force it to focus
    }
}

Upvotes: 0

Related Questions