title: C# Datagrid (not datagridview): how to sort a column of numbers since all values are stored as Text?
body: <p>I am fairly new to C#.  I have a datagrid object.  First column is a name column as text of course; all other columns are (below the header row) are numbers.  When I click on one of the column headers to sort that column, I see that is is sorting the numbers as strings since they are stored that way.  How do I get the sorting to sort by numeric value instead?  Do I need to write my own sorting routine? Do I need a converter for that?  If so, can anyone point me to an example of how that is done?  </p>
  
  <p>Here is how I am adding the data to my DataGrid:</p>
  
  <pre><code>rows = dbm.getDataForRow(tableview, supid, true);
  DataGridTextColumn c1 = new DataGridTextColumn();
  TextBlock tbHeader = new TextBlock();
  
  if (suplier == "All")
      tbHeader.Text = "Suplier ";
  else
      tbHeader.Text = "Date";
  
  tbHeader.LayoutTransform = new RotateTransform(-90);
  tbHeader.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  c1.Header = tbHeader;
  c1.Binding = new Binding("rowdata[0]");
  dg.Columns.Add(c1);
  string cellvalue = null;
  for (int i = 1; i &lt; columnnames.Count + 1; i++)
  {
     tbHeader = new TextBlock();
     c1 = new DataGridTextColumn();
     tbHeader.Text = columnnames[i - 1];
     tbHeader.LayoutTransform = new RotateTransform(-90);
     tbHeader.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
     c1.Header = tbHeader;
  
     c1.Binding = new Binding("rowdata[" + i + "]");
     // or should I just add the data like c1.Columns.Add(rowdata[i]);
     dg.Columns.Add(c1);
  }
  
  dg.ItemsSource = rows;
  </code></pre>