Skip to main content Skip to footer

Merging Cells in DatagridHyperLinkColumn

Merging of cells within C1DataGrid for Silverlight is something that every user does. And MergingHelper class available with C1DataGrid help merge cells in all types of columns. However, it fails when it comes to merge cells in DatagridHyperLinkColumn. Say, there's a C1Datagrid, bound to a Collection, with a DatagridHyperLinkColumn and a DataGridTextColumn. Also, we assume DatagridHyperLinkColumn contains some URL(s)/Text which may be merged according to values in cells of DataGridTextColumn. For e.g: DataGridTextColumn contains 1,2,3,3,4,5,6,6 and we need merging to be implemented only on rows containing 3,3 and 6,6; well, wherever possible. Something like this screenshot: The grid looks good with merging logic implemented. Cells in columns "Select" and "ID" got merged too. However, I don't see any merging within DatagridHyperLinkColumn. Reason, GetCellValue() method in MergingHelper class doesn't handle a condition where DataGridCellPresenter is actually the DataItem. Check out the original code:


public static object GetCellValue(DataGridCell cell)  
{  
   // We used the binding here previously, but that doesn't work for column headers.  
   object content = cell.Presenter;  
   while (true)  
   {  
      if (content is ContentControl)  
      {  
          content = (content as ContentControl).Content;  
      }  
      else if (content is TextBlock)  
      {  
          return (content as TextBlock).Text;  
      }  
      else if (content is TextBox)  
      {  
          return (content as TextBox).Text;  
      }  
      else  
      {  
          return content;  
      }  
  }  
}  

And, here's modified code to take care of the aforementioned situation.


public static object GetCellValue(DataGridCell cell)  
{  
  // We used the binding here previously, but that doesn't work for column headers.  
  object content = cell.Presenter;  
  while (true)  
  {  
     if (content is ContentControl)  
     {  
        content = (content as ContentControl).Content;  
     }  
     else if (content is TextBlock)  
     {  
        return (content as TextBlock).Text;  
     }  
     else if (content is TextBox)  
     {  
        return (content as TextBox).Text;  
     }  
     // If DatagridCellPresenter is DataItem, return the Property  
     else if (content is SampleClass)  
     {  
        return (content as SampleClass).Link1;  
     }  
     else  
     {  
        return content;  
     }  
  }  
}  

And, this is how grid looks like after merging implemented on DatagridHyperLinkColumn. Refer the attached sample for complete implementation. Download Sample

MESCIUS inc.

comments powered by Disqus