I am stuck with finding a solution for this scenario:
I have two datagrids (DevExpress). On left-side grid I have some templates values (rows). I have one move button between two grids, which on click event should perform moving selected item from left grid to right grid. Before it copy that value it should check does maybe that value already exists in right grid. If does: Show message that it already exists, if not, add selected value to right grid.
In code I have collections that are holding this values:
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
- is left side grid. gridViewTemplatesToPrint
- is right side grid.
What I am doing in this code is:
- Get currently selected row in left grid:
- Create new collection where I will add all items from right grid (I will use this later for comparing and checking what I have already selected).
- Add items to temporary collection with looping through right grid rows.
- Checking does
currentTemplateList
contains selected value. - If yes, show message, if not add it to right grid by setting it's
DataSource
tocurrentTemplateList.Items
.
The problem happens here:
ModelCollection<ClosingStockTemplateModel> currentTemplateList = new ModelCollection<ClosingStockTemplateModel>();
I need to make and instance of ModelCollection
, but every time I click button Move list gets instantiated and it is cleared. So I've tried something like this:
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);
}
}
Only to make a clean instance of currentTemplateList
when move button is clicked for the first time and right side grid is still empty, but then I have problem with currentTemplateList
being unknown local variable in else
part. Is there some clean way to solve this? Because with this code, new items are not added to right sided grid, they are cleaned every time and on the right side I have only last moved item. I need to find a way to persist already selected items in collection when grid was not empty.
Aucun commentaire:
Enregistrer un commentaire