title: How can i get a string value from a datagrid cell?
body: <p>this is my first post. I'm new to C# WPF so i might be completely off in my thinking. I'm basically trying to create a datagrid that acts like spreadsheet so that when the user inputs a sum, eg. 10 * 10, on CellEditEnding it will show the answer in the cell like excel.</p>
  
  <p>I have a 6x6 grid bound to an empty ObservableCollection and when the user types in the 10 * 10 i'm trying to take the string they entered, split it, and then use the parts to create a calculation but at the minute i cant get the string from the cell. The calculation works when i set the string directly in the code.</p>
  
  <p>Any help would really be appreciated. Perhaps there is an easier way to do this.</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      ObservableCollection&lt;DataCell&gt; spreadsheet = new ObservableCollection&lt;DataCell&gt;();
  
      public MainWindow()
      {
          //Communal Spreadsheet
          InitializeComponent();
  
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
          spreadsheet.Add(new DataCell("", "", "", "", "", ""));
  
          dataGrid2.ItemsSource = spreadsheet;
  
  }
  
  private void dataGrid2_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
      {
          // I want to get the current cell value using the below
          // string myString = dataGrid1.CurrentItem.ToString();
  
          // the calculation seems to work with this when i pass it to textbox1 and set the myString Variable in the code.   
          string myString = "10 * 10";
  
          string[] parts = myString.Split(' ');
  
          int part1 = int.Parse(parts[0]);
          //    string part2 = parts[1];
          int part3 = int.Parse(parts[2]);
  
          int answer = part1 * part3;
          string answer2 = answer.ToString();
  
          // dataGrid2.CurrentItem = answer2;
  
          // seems to work when i pass to the below textbox instead of back to the cell above 
          textBox1.Text = answer2;
      }
  }
  </code></pre>
  
  <p>}</p>