title: How to group rows in datagridview with subheadings in vb.net
body: <p>I'm having trouble on how to groups rows in datagridview with a sub-headings in vb.net. i want my datagridview to have a subheading. i've converted this C# Code into VB.NET but nothing happens:</p>
  
  <pre><code>private string subheading = "";
  private bool createsub = false;
  // This method will peek ahead in the datasource to determine whether the 
     next
   // item will need a new subheading
   private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs 
  e)
  {
  switch (e.Item.ItemType)
  {
  case ListItemType.Header:
  case ListItemType.AlternatingItem:
  case ListItemType.Item:
   DataTable dt = (DataTable)((DataGrid)sender).DataSource;
  // header or Subheading column value will change in next row
  if ((e.Item.ItemIndex == -1) ||
  (e.Item.ItemIndex+1 &lt; dt.Rows.Count &amp;&amp;
  dt.Rows[e.Item.ItemIndex+1]["SubHeading"].ToString() !=
  dt.Rows[e.Item.ItemIndex]["SubHeading"].ToString()))
  {
   // get next subheading value
   subheading = dt.Rows[e.Item.ItemIndex+1]["SubHeading"].ToString();
   createsub = true;
   }
  else
  createsub = false;
  break;
  }
  }
  // This method will create a subheading row if needed
  private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
  {
  switch (e.Item.ItemType)
  {
  case ListItemType.AlternatingItem:
  case ListItemType.Item:
  if (createsub)
  {
  DataGrid dg = (DataGrid)sender;
  TableCell tc = new TableCell();
  tc.Controls.Add(new LiteralControl(subheading));
  tc.ColumnSpan = e.Item.Cells.Count;
  tc.Attributes.Add("align", "left");
  tc.Font.Bold = true;
  tc.BackColor = Color.FromArgb(220, 220, 220);
  DataGridItem di =
  new DataGridItem(e.Item.ItemIndex+1, 0, ListItemType.Item);
  di.Cells.Add(tc);
  Table t = (Table)dg.Controls[0];
  t.Rows.Add(di);
  }
  break;
  default:
  break;
  }
  }
  </code></pre>
  
  <p>And this code for additional info with the same result:</p>
  
  <pre><code>private void SeparateRows(ref DataSet ds, string columnName)
  {
  int i = 0;
  string prevsub = "";
  while (i &lt;= ds.Tables[0].Rows.Count - 1)
  {
  DataRow dr = ds.Tables[0].Rows[i];
  // if category field value changes add a new row
  if (dr["Category"].ToString() != prevsub)
  {
  prevsub = sub;
  DataRow newrow = ds.Tables[0].NewRow();
  newrow["Title"] = "SubHeading"; // sub heading flag
  newrow[columnName] = dr[columnName]; // sub heading text
  // add row and increment counter to accommodate new row
  ds.Tables[0].Rows.InsertAt(newrow, i++);
  }
  i++;
  }
  }
  
  
  private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
  {
  switch (e.Item.ItemType)
  {
  case ListItemType.AlternatingItem:
  case ListItemType.Item:
  if (e.Item.Cells[1].Text.Equals("SubHeading"))
  </code></pre>
  
  <p>Try to open this image i uploaded to be clarified:</p>
  
  <p><a href="https://i.stack.imgur.com/C6vkN.jpg" rel="nofollow noreferrer">Grouping Datagridview rows</a></p>
  
  <p>I really need some help achieving this. Please, Help.. Thanks</p>