2016-03-30 12 views
0

Sorun oldukça basittir. Benim datagrid temelde dizelerle dolu nesnelerin bir listesi olan ItemSource (bindingList) öğesinden doldurulur.DataGrid'de Satır Seçimlerini Kaydetme ve Geri Yükleme

Kodumun bu belirli kısmında, bindingList'imi güncellemem gerekiyor. Ne yazık ki güncellendiğinde DataGrid'de yapılan tüm kullanıcı sıra seçimleri yok olur.

Bu, kullanıcı için düzeltmek istediğim bir rahatsızlıktır. Kullanıcı, tıklatılacak ciltleme listesi ile sonuçlanan düğmeyi tıkladığında, kullanıcının daha fazla değişiklik yapmak istemesi durumunda seçimler kaydedilir.

Kod Right Now:

//Save DataGrid Row Selections 
bindingList[1] = (new ItemClass() { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip }); 
dataGrid.ItemSource = bindingList; 
//Restore DataGrid Row Selections 

DÜZENLEME: Geniş kapsamı istendiği gibi:

private void Y_Incre_Button_Click(object sender, RoutedEventArgs e) 
{ 
    if (dataGrid.SelectedItems.Count != 0) 
    { 
     string colonum; 
     string xpos; 
     string ypos; 
     string descrip; 

     for (int i = 0; i < bindingList.Count; i++) 
     { 
      int selectionIndex = dataGrid.SelectedIndex; 

      if (selectionIndex > -1) 
      { 
       var curItem = bindingList[selectionIndex]; 
       int yNum = int.Parse(curItem.ycoord); 
       int yNum2 = (yNum + 10); 
       colonum = curItem.columnnumber; 
       xpos = curItem.xcoord; 
       ypos = yNum2.ToString(); 
       descrip = curItem.description; 

       //Save DataGrid Row Selections 
       bindingList[selectionIndex] = (new ItemClass() { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip }); 
       //Restore DataGrid Row Selections 
      } 
     } 
    } 
    else 
    { 
     MessageBox.Show("No Rows Selected"); 
    } 
} 
+1

Eğer ObservableCollection öğeleri daha sonra, bir ObservableCollection için ItemSource set takmak/silmek için bağlayıcı (yerine toplanması yerine doğru XAML kullandıysanız – aguertin

+1

(şimdiye 15 satırları veya çevredeki) lütfen biraz daha fazla kod gösterir misiniz) o zaman ne istersen başarabilirsin. – slugster

+0

Öğeleri eklemiyorum veya silmiyorum, sadece seçilen öğeyi güncelliyorum (değiştirerek). Muhtemelen ObservableCollection'da farklı mı yapıldı? – Mercender

cevap

1

seçilen öğelerin satır endeksleri onlar değiştirilir önce bu çalışma mağaza olsun, sonra yeniden seçin "Değiştir" işleminden sonra bu kayıtlar tamamlandı. Ayrıca, this örneğine bakın.

private void Y_Incre_Button_Click(object sender, RoutedEventArgs e) 
{ 
    if (dataGrid.SelectedItems.Count != 0) 
    { 
     // Save DataGrid Row Selections 
     List<int> selectedRowIndexList = new List<int>(); 
     foreach (object item in dataGrid.SelectedItems) 
     { 
      selectedRowIndexList.Add(dataGrid.Items.IndexOf(item)); 
     } 

     for (int i = 0; i < bindingList.Count; i++) 
     { 
      int selectionIndex = dataGrid.SelectedIndex; 

      if (selectionIndex > -1) 
      { 
       ItemClass curItem = bindingList[selectionIndex]; 

       int yNum = int.Parse(curItem.ycoord); 

       int yNum2 = yNum + 10; 

       string colonum = curItem.columnnumber; 
       string xpos = curItem.xcoord; 
       string ypos = yNum2.ToString(); 
       string descrip = curItem.description; 

       bindingList[selectionIndex] = new ItemClass { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip }; 
      } 
     } 

     // Restore DataGrid Row Selections 
     dataGrid.SelectedItems.Clear(); 
     foreach (int rowIndex in selectedRowIndexList) 
     { 
      if (rowIndex < 0 || rowIndex > dataGrid.Items.Count - 1) 
       throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex)); 

      object item = dataGrid.Items[rowIndex]; 
      dataGrid.SelectedItems.Add(item); 
     } 
    } 
    else 
    { 
     MessageBox.Show("No Rows Selected"); 
    } 
} 
+0

Mükemmel çalışıyor. Otostopladığım bölüm, SelectedItems'in Add üyesi olduğunu bilmiyordum. Teşekkür ederim! – Mercender

İlgili konular