2016-03-30 8 views
0

問題はかなり簡単です。私のDataGridは、基本的に文字列でいっぱいのオブジェクトのリストである私のItemSource(bindingList)からいっぱいです。DataGridの行選択の保存と復元

私のコードのこの特定の部分で、私は自分のbindingListを更新する必要があります。 残念ながら、更新されると、DataGridで行われたすべてのユーザー行の選択が消えます。

これは、私が是正したいと思っている不便さです。そのため、ユーザーがbindingListを更新するボタンをクリックしたときに、ユーザーがさらに変更したい場合に備えて、選択内容が保存されます。

コード今すぐ右:

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

EDIT: より広い範囲の要求に応じて:

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

は、あなたがのObservableCollectionにアイテムを、その後、のObservableCollectionにあなたのItemSourceを設定する挿入/削除するには、結合(代わりのコレクションを交換し、適切なXAMLを使用した場合 – aguertin

+1

(その15行または周囲の)してくださいもう少しコードを示すことができました)あなたが望むものを達成するかもしれません。 – slugster

+0

私はアイテムを挿入または削除するのではなく、選択したアイテムを(それを置き換えることによって)更新するだけです。それはおそらくObservableCollectionで別々に行われますか? – Mercender

答えて

1

は、選択した項目の行インデックスは、彼らが交換される前にこの作業ストアを取得し、その後、再選択します「置換」操作が完了したレコードは完了します。また、thisの例を見てください。

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

完全に動作します。私は、SelectedItemsがAddメンバーを持っているかどうかわからなかった。ありがとうございました! – Mercender

関連する問題