0

Suppose I have a DataGrid and a Button. CanUserAddRows is set to True. Let this dataGrid have two DataGridTextColumns namely "Name" and "Age".

Now if user takes the following actions :

  1. He adds name as XYZ and Age as 20. Then he press enter. So a new row will be added to the datagrid.
  2. He adds name as ABC and Age as 12. Then he press enter. So a new row will be added to the datagrid.
  3. He keeps name empty and press Enter or TAB then I want to move focus to Button instead of next cell of datagrid.

I have watched many questions but I don't get the logic of if user left the name empty and how do I move focus to Button instead of next cell.

Vishal
  • 6,238
  • 10
  • 82
  • 158

2 Answers2

2

Use DataGridView.SelectedCells[0] so you can retrieve the value of the selected cell (assuming you can only select one).

To get the actual string inside, you will have to cast the content to a proper WPF object, like TextBlock. myCell.Column.GetCellContent(cell.Item) as TextBlock

Then in a PreviewKeyDown event handler (KeyDown having known issues in DataGridView), you can use button.Focus(). (more about those issues)

//...
myDataGrid1.PreviewKeyDown += myDataGrid1_KeyDown;
//...
void myDataGrid1_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        var cell = myDataGrid1.SelectedCells[0];
        TextBlock cellContent = cell.Column.GetCellContent(cell.Item) as TextBlock;
        if (cellContent != null)
        {
            if (String.IsNullOrWhitespace(cellContent.Text))
                button.Focus();
        }
    }
}

About getting the column's name, it's another question, for which you can find answer here for example.

As a side note, you're not really supposed to interact directly with a DataGridView cells' values, since it's meant to be bound with a data source from which you should retrieve the data you want to test. However, you can search a bit for helper methods that can help you get what you want.

Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • what is `cell`? WPF does not recognize it. – Vishal May 13 '14 at 12:22
  • Sorry to say but I get another error : Cannot implicitly convert FrameworkElement to String – Vishal May 13 '14 at 13:32
  • The event is never fired. Here is the sample project https://drive.google.com/file/d/0B5WyqSALui0bZnZtd2doZklUSGs/edit?usp=sharing Please take a look at it when you become free. – Vishal May 13 '14 at 15:43
  • You can also google the issue and tell me what you found. It's apparently a known issue with KeyDown. Gotta use PreviewKeyDown instead. Editing. – Kilazur May 13 '14 at 16:18
  • Sorry for the late reply. I have been out of town. I have tried PreviewKeyDown instead of KeyDown as you suggested. Now event fires correctly but `cellContent is always null`. – Vishal May 18 '14 at 15:25
  • Looks like there is an issue about getting content froms cells in DataGridView (values not updated soon enough to get grabbed), so [check the answers here](http://stackoverflow.com/questions/8464704/get-row-in-datagrid), I'm sure you'll find what you need. – Kilazur May 19 '14 at 07:32
0

You can define a handler for the DataGrid.KeyDown event, as:

void myDataGrid1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        button.Focus();
    }
}
Mitulát báti
  • 2,086
  • 5
  • 23
  • 37
  • But how do I know that the cell belongs to Name Column and If is this cell is empty or not? – Vishal May 13 '14 at 11:22