title: How to sort databound DataGridView column?
body: <p>I know that there are a lot of questions on this topic. I have been through all of them but nothing seems to help.</p>
  
  <p>How to sort by clicking on column header?</p>
  
  <p>How should I modify this code to do the job?</p>
  
  <pre><code>public partial class Form1 : Form
  {
  
      public Form1()
      {
  
          List&lt;MyClass&gt; list = new List&lt;MyClass&gt;();
          list.Add(new MyClass("Peter", 1202));
          list.Add(new MyClass("James", 292));
          list.Add(new MyClass("Bond", 23));
  
          BindingSource bs = new BindingSource();
          bs.DataSource = list;
  
          DataGridView dg = new DataGridView();
  
          DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
          c.Name = "name";
          c.DataPropertyName = "Name";
          dg.Columns.Add(c);
  
          c = new DataGridViewTextBoxColumn();
          c.Name = "number";
          c.DataPropertyName = "Number";
          dg.Columns.Add(c);
  
          dg.DataSource = bs;
  
          this.Controls.Add((Control)dg);
  
      }
  
  }
  
  class MyClass:IComparable&lt;MyClass&gt;
  {
      public string Name { get; set; }
      public int Number { get; set; }
  
      public MyClass(){}
  
      public MyClass(string name,int number)
      {
          Name = name;
          Number = number;
      }
  
      public override string ToString()
      {
          return string.Format("{0}:{1}",Name,Number);
      }
  
      #region IComparable&lt;MyClass&gt; Members
  
      public int CompareTo(MyClass other)
      {
          return Name.CompareTo(other.Name);
      }
  
      #endregion
  }
  </code></pre>