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.