2011/12/05

Get Any Cell Value from a selected row/cell In .NET

Sometimes in your application you need to do a very simple thing like selecting a row in a datagrid to get a cell value, but in .NET's DataGridView will highlight the cell when you click on it and not the row or column.

So why do you have to select a whole row to get a value, or why do you have to select the exact cell i want?
there is a very simple code that accepts any selected cell/row to get your needed cell in that row - check the image:
as you can see I clicked on "Mataderos 2312" cell and got the result "ANTON" which is the value I want, without the need of selecting the whole row or selecting the exact cell needed, just any cell in that row.
code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim str As String = DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells(0).Value
TextBox1.Text = str
End Sub


code description:
to get the selected item:
DataGridView1.SelectedCells.Item(0).Value
to get it's row index
DataGridView1.SelectedCells.Item(0).RowIndex
to get a cell for it's row
DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex).Cells(0).Value

Programming your own Speech Recognition

Speech Recognition
I have a task to develop our own dictionary in windows Speech Recognition (in Windows7), which recogize only our words without having/using the normal (conversation) words that are used in Windows' Dictionary.
and when i was developing that, i did added my own words (as this helpful example), but still the program accepts all kind of words, and i don't want that.
While (hard) searching throught the net, it appeared that this is not possible to have my own dictionary as one said the following:

The shared recogniser which turns on the Windows command recognition grammar. and You can't avoid it, this is disappointing and frustrating. The solution is the second option, creating your own recogniser.
You do not need to turn on the Windows Speech recognition, instead you end up with your own recognition engine, completely independant of the Windows command recogniser.
This MSDN guide is for using WAV -> SR, but most of it applies:
http://msdn.microsoft.com/en-us/library/ms717071%28VS.85%29.aspx
Replace the Stream object wit han spAudio object and you're almost there, here's the Audio Object Code(shortened, no HRESULT checks):
CComPtr pAudioToken;
CComPtr pAudio;

// Try the default
hr = SpGetDefaultTokenFromCategoryId(SPCAT_AUDIOIN, &pAudioToken);


// Connect the Device
hr = pRecognizerEngine->SetInput(pAudioToken, TRUE);

hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &pAudio);
hr = pRecognizerEngine->SetInput(pAudio, TRUE);

After that you can continue using the MSDN link above, and will be almost finished, except you need to manually activate the Audio Stram from a microphone / Line-In:
hr = pGrammar->SetGrammarState(SPGS_ENABLED);
hr = pNavGrammar->SetDictationState(SPRS_ACTIVE);
Assuming you are careful in checking the errors, you'll have your own voice recognition, and NO windows commands going off!

Links:
  1. Windows Speech Recognition: How to Benefin from its Advanced Configuration Options
  2. Grammar Format Tags - to create your own grammar (dictionary) as this solitaire example
  3. How to disable windows 7 speech recognition COMMANDS