2017-09-28 6 views
1

このシナリオの解決策を見つけることに悩まされています:Cのリストに項目を残してください

私は2つのデータグリッド(DevExpress)を持っています。左側のグリッドには、いくつかのテンプレート値(行)があります。クリックイベントでは、選択した項目を左グリッドから右グリッドに移動する2つのグリッド間に1つの移動ボタンがあります。その値をコピーする前に、その値がすでに正しいグリッドに存在するかどうかを確認する必要があります。 If:既存のメッセージを表示します。存在しない場合は、選択した値を右のグリッドに追加します。 - 左側のグリッドである

private void btnMove_Click(object sender, EventArgs e) 
{ 
    ClosingStockTemplateModel _mClosingStockTemplateModel = (ClosingStockTemplateModel)gridViewAvailableTemplates.GetRow(gridViewAvailableTemplates.FocusedRowHandle); 
    ClosingStockTemplateModel currentTemplateRow = new ClosingStockTemplateModel(); 
    ModelCollection<ClosingStockTemplateModel> currentTemplateList = new ModelCollection<ClosingStockTemplateModel>(); 

    for (var i = 0; i < currentTemplateList.Items.Count; i++) 
    { 
     currentTemplateRow = (ClosingStockTemplateModel)gridViewTemplatesToPrint.GetRow(i); 
     currentTemplateList.Items.Add(currentTemplateRow); 
    } 

    if (currentTemplateList.Items.Contains(_mClosingStockTemplateModel)) 
    { 
     MessageBox.Show("Item has already been selected."); 
     return; 
    } 
    else 
    { 
     currentTemplateList.Items.Add(_mClosingStockTemplateModel); 
     grdTemplatesToPrint.DataSource = null; 
     grdTemplatesToPrint.DataSource = currentTemplateList.Items.OrderBy(i => i.TEMPLATE); 
    } 
} 

gridViewAvailableTemplates:コードで

私はこの値を保持しているコレクションを持っています。 gridViewTemplatesToPrint - 右側のグリッドです。私はこのコードでやっている何を

は次のとおりです。

  1. 左グリッドで現在選択されている行を取得します:
  2. (私は比較のために、この以降を使用します、私は右のグリッドからすべての項目を追加する新しいコレクションを作成します。私がすでに選択したものをチェックする)。
  3. 右グリッドの行をループして、一時的なコレクションにアイテムを追加します。
  4. チェックはcurrentTemplateListに含まれています。
  5. の場合は、DataSourcecurrentTemplateList.Itemsに設定してメッセージを表示します(右グリッドに追加しない場合)。

問題はここに起こる:

ModelCollection<ClosingStockTemplateModel> currentTemplateList = new ModelCollection<ClosingStockTemplateModel>(); 

私はModelCollectionの作成およびインスタンスする必要がありますが、私はボタンの移動リストをクリックするたびにインスタンス化されますし、それがクリアされています。移動ボタンを初めてクリックすると、右側のグリッドがまだ空であるが、その後、私はcurrentTemplateListが不明であることに問題があるされている場合にのみcurrentTemplateListのきれいなインスタンスを作るために

ModelCollection<ClosingStockTemplateModel> currentTemplateList; 
if(gridViewTemplatesToPrint.RowCount == 0) 
{ 
    currentTemplateList = new ModelCollection<ClosingStockTemplateModel>(); 
} 
else 
{ 
    for (var i = 0; i < currentTemplateList.Items.Count; i++) 
    { 
     currentTemplateRow = (ClosingStockTemplateModel)gridViewTemplatesToPrint.GetRow(i); 
     currentTemplateList.Items.Add(currentTemplateRow); 
    } 
} 

:だから私はこのような何かを試してみましたローカル変数elseにあります。 これを解決するいくつかのきれいな方法がありますか?このコードでは、新しいアイテムは右側のグリッドに追加されないため、毎回削除され、右側には最後に移動したアイテムしかありません。私は、グリッドが空でないときにコレクション内の既に選択されたアイテムを永続化する方法を見つける必要があります。あなたはDataSourceを毎回設定する必要はありません

+4

MVVMを使用し、viewmodelで実行します。 –

答えて

0

クラスレベルで宣言し、次のフィールドと、それは静的作る....と あなたが必要な場所にそれを初期化する...

> ModelCollection<ClosingStockTemplateModel> currentTemplateList = new 
> ModelCollection<ClosingStockTemplateModel>(); 
0

DataSourceをコレクションに1回セットしてから、リストまたはコレクションのみを使用してアイテムを追加または削除します。ここ あなたは、一般的なBindingListの代わりListを使用している場合は、RefreshDataSource呼び出しを削除することができます簡単な例(2グリッドの2ボタンの)

private List<ClosingStockTemplateModel> moTemplatesToPrintList; 
private List<ClosingStockTemplateModel> moAvailableTemplates; 

public Form4() 
{ 
    InitializeComponent(); 

    this.moAvailableTemplates= new List<ClosingStockTemplateModel>() 
    { 
     new ClosingStockTemplateModel() {ID = 1, TEMPLATE ="Template 1" }, 
     new ClosingStockTemplateModel() {ID = 2, TEMPLATE ="Template 2" }, 
     new ClosingStockTemplateModel() {ID = 3, TEMPLATE ="Template 3" }, 
     new ClosingStockTemplateModel() {ID = 4, TEMPLATE ="Template 4" }, 
    }; 

    this.moTemplatesToPrintList = new List<ClosingStockTemplateModel>(); 

    this.grdAvailableTemplates.DataSource = this.moAvailableTemplates; 
    this.grdTemplatesToPrint.DataSource = this.moTemplatesToPrintList; 
} 

private void cmdAdd_Click(object sender, EventArgs e) 
{ 
    var loGridViewAvailableTemplates = (this.grdAvailableTemplates.MainView as GridView); 
    ClosingStockTemplateModel loClosingStockTemplateModel = loGridViewAvailableTemplates.GetRow(loGridViewAvailableTemplates.FocusedRowHandle) as ClosingStockTemplateModel; 

    if (loClosingStockTemplateModel != null && 
     !this.moTemplatesToPrintList.Any(item => item.ID == loClosingStockTemplateModel.ID)) 
    { 
     this.moTemplatesToPrintList.Add(loClosingStockTemplateModel); 
     this.moAvailableTemplates.Remove(loClosingStockTemplateModel); 
    } 
    this.grdAvailableTemplates.RefreshDataSource(); 
    this.grdTemplatesToPrint.RefreshDataSource(); 
} 

private void cmdRemove_Click(object sender, EventArgs e) 
{ 
    var loGridViewTemplatesToPrint = (this.grdTemplatesToPrint.MainView as GridView); 
    ClosingStockTemplateModel loClosingStockTemplateModel = loGridViewTemplatesToPrint.GetRow(loGridViewTemplatesToPrint.FocusedRowHandle) as ClosingStockTemplateModel; 

    if (loClosingStockTemplateModel != null && 
     !this.moAvailableTemplates.Any(item => item.ID == loClosingStockTemplateModel.ID)) 
    { 
     this.moAvailableTemplates.Add(loClosingStockTemplateModel); 
     this.moTemplatesToPrintList.Remove(loClosingStockTemplateModel); 
    } 
    this.grdAvailableTemplates.RefreshDataSource(); 
    this.grdTemplatesToPrint.RefreshDataSource(); 
} 

public class ClosingStockTemplateModel 
{ 
    public int ID { get; set; } 
    public string TEMPLATE { get; set; } 
} 

です。

関連する問題