Sanal Rajan

Sanal Rajan

  • NA
  • 174
  • 2.9k

Silverlight:DataGrid-Exception - Index must be within bounds

Jun 19 2017 3:11 AM

Silverlight:DataGrid-Exception - Index must be within the bounds of the List. Parameter name: index after grouping using PagedCollectionView :

 When executing this code at first time it is working. second time onwards throwing exception " Index must be within the bounds of the List" . If i remove " itemListView.GroupDescriptions.Add " data is showing in the grid. Without removing this line how to solve this.. 
 
Codes are here....
 
 
 ObservableCollection<string> RepcolumnName = new ObservableCollection<string>(); 
RepcolumnName.Add(ResourceFile.SrNoLevel);
 RepcolumnName.Add(ResourceFile.EmpCodeLabel); 
RepcolumnName.Add(ResourceFile.EmployeeNameLabel); 
Binding binding = new Binding(); 
int index = 0; string bindCol = ""; 
foreach (var header in RepcolumnName) 
{  
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn(); 
 templateColumn.Header = header; 
if (index == 0)
 {     bindCol = "sNo"; } 
else if (index == 1) {  bindCol = "Empno"; } 
else if (index == 2) {  bindCol = "Name"; } 
 templateColumn.HeaderStyle = LayoutRoot.Resources["headerStyle"] as Style; 
StringBuilder CellTemp = new StringBuilder(); 
CellTemp.Append("<DataTemplate "); 
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/"); 
CellTemp.Append("2006/xaml/presentation' "); 
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
 CellTemp.Append("xmlns:local = 'clr-namespace:System.Windows.Controls");
 CellTemp.Append(";assembly=System.Windows.Controls.Toolkit'>"); 
CellTemp.Append("<Grid>"); CellTemp.Append("<TextBlock "); 
CellTemp.Append("Text ='{Binding Path=" + bindCol + "}'  Style='{StaticResource ContentTextStyle}' IsHitTestVisible='False' ");
 CellTemp.Append("Margin='4'/>"); 
CellTemp.Append("</Grid>"); 
CellTemp.Append("</DataTemplate>");  
templateColumn.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());
 DgvEmpData.Columns.Add(templateColumn);         
 index++; } 
PagedCollectionView itemListView = new PagedCollectionView(FinalEmpList);
 DgvEmpData.ItemsSource = itemListView; 
if (itemListView.CanGroup == true) {            
  itemListView.GroupDescriptions.Add(new PropertyGroupDescription("DeptName"));
} 


 

Answers (2)