title: C# WPF DataGrid Search for a Value in a Column, Return the Row Index
body: <p>I am new at C# development.  I am learning and struggling as I am writing an application - the best way to learn is by doing : )</p>
  
  <p>I hope that someone will be able to assist me with my issue.</p>
  
  <p>I am populating the <strong>WPF DataGrid</strong> via the <strong>dataGrid1.ItemsSource = dt.DefaultView;</strong> 
  and then letting the DataGrid automatically generate the columns for me.</p>
  
  <p>The user is able to click on any row in the DataGrid which will then populate the header section that I have on the WPF UI with the row's data - this allows the user to edit the row via the header.  I do not want them to edit via the DataGrid.</p>
  
  <p>The user is able to edit the row via the header fields and then click on an UPDATE button.  The UPDATE button will run a stored procedure which handles all validations and updating of the record.  Once the record is saved I fire off the grid refresh method.  </p>
  
  <p>Once the grid is refreshed I need to be able to search on a specific column on the DataGrid in order to Select, set Focus and scroll into the row that just got updated. </p>
  
  <p>I have searched like crazy on Google and I just cannot find how to do this on the DataGrid.  There are samples on how to do it on the DataGridView which is not what I am using.</p>
  
  <p>Any help is greatly appreciated....thanks</p>
  
  <h2><strong>This is my code for the Button Click</strong></h2>
  
  <p>private void btn_Update_Click(object sender, RoutedEventArgs e)
          {</p>
  
  <pre><code>        // variables
          bool isOkToUpdate = true;
  
          this.Cursor = Cursors.Wait;            
  
          // Validations of certain fields
          if (txt_FinanceEmpName.Text.Length &gt; 25 )
          {
              MessageBox.Show("The Finance Emp Name must not exceed 25 characters in length. " 
                  + txt_FinanceEmpName.Text.Length.ToString()
                  , "Maximum characters exceeded"
                  , MessageBoxButton.OK, MessageBoxImage.Stop);
  
              isOkToUpdate = false;
          }
  
          //
          if (isOkToUpdate == true)
          {
              // create an instance
              DatabaseClass objDatabaseClass = new DatabaseClass(_connectionString);
  
              // if we are able to open and close the SQL Connection then proceed
              if (objDatabaseClass.CheckSQLConnection())
              {
                  try
                  {
                      // create instance of SqlConnection class. variable 'con' will hold the instance
                      SqlConnection con = new SqlConnection(_connectionString);
  
                      con.Open();
  
                      // use a using for all disposable objects, so that you are sure that they are disposed properly
                      using (SqlCommand cmd = new SqlCommand("usp_mktdata_update_cm_mktdata_emp_name_fits_to_finance", con))
                      {
                          cmd.CommandType = CommandType.StoredProcedure;
                          cmd.Parameters.Add("@IdUnique", SqlDbType.Int).Value = Convert.ToInt32(txt_IdUnique.Text);
                          cmd.Parameters.Add("@FinanceEmpName", SqlDbType.VarChar).Value = txt_FinanceEmpName.Text;
                          cmd.Parameters.Add("@ADWindowsLogon", SqlDbType.VarChar).Value = _windowsUserName;
                          cmd.Parameters.Add("@ADWindowsLogonEMail", SqlDbType.VarChar).Value = _windowsUserNameEMail;
                          cmd.Parameters.Add("@IndActive", SqlDbType.TinyInt).Value = 1;
                          cmd.Parameters.Add("@RecordVersion", SqlDbType.Int).Value = Convert.ToInt32(txt_RecordVersion.Text);
                          //cmd.Parameters.Add("@IdJob", SqlDbType.Int).Value = "DEFAULT";
                          //cmd.Parameters.Add("@IdUser", SqlDbType.Int).Value = "DEFAULT";
                          //cmd.Parameters.Add("@outIdUnique", SqlDbType.Int).Value = "DEFAULT";
                          //cmd.Parameters.Add("@outRecordVersion", SqlDbType.Int).Value = "DEFAULT";
                          //cmd.Parameters.Add("@outInd", SqlDbType.Int).Value = "DEFAULT";
                          //cmd.Parameters.Add("@outMessage", SqlDbType.VarChar).Value = "DEFAULT";
  
                          cmd.ExecuteNonQuery();
                      }
  
                      // Last Updated Record
                      txt_LastUpdated_IdUnique.Text = txt_IdUnique.Text;
                      txt_LastUpdated_FitsEmpName.Text = txt_FitsEmpName.Text;
                      txt_LastUpdated_FinanceEmpName.Text = txt_FinanceEmpName.Text;
  
                      // Refresh the Datagrid - the DataGrid_MonikerName
                      // pass in null for the params in order to fire the event
                      btn_RefreshGrid_Click(null, null);
  
  
  
                      // ****************************************
                  // TODO:  After Grid Refresh Search the column ID UNIQUE for the value stored in txt_IdUnique.Text which
                  //  was just saved via the stored procedure
                  // Once the value is found in the DataGrid need to grab the DataGrid Row INdex in order to 
                  // Select the ROW and Set the ROW to the SELECTED ROW and set focus to the DataGrid also will need to Scroll the Grid into View.
                      //
                      // ****************************************
  
  
                      con.Close();
                  }
                  catch (SqlException ex)
                  {
                      MessageBox.Show(ex.ToString());                    
                  }
              }
              else
              {
                  MessageBox.Show("Connection not established to the SQL Server. " + Environment.NewLine + "The SQL Server may be offline or valid credentials are not yet granted.", "SQL Server Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
  
                  this.Close();
              }
          }   
  
          this.Cursor = Cursors.Arrow;
      }
  </code></pre>