Пример #1
0
 public static void PaintDragCells(DataGrid datagrid, CellCopyInfo copyInfo, bool dragActive)
 {
     try
     {
         for (int i = 0; i < datagrid.Items.Count; i++)
         {
             for (int j = 0; j < datagrid.Columns.Count; j++)
             {
                 DataGridCell cell = GetCell(datagrid, i, j);
                 if (null != cell)
                 {
                     TextBlock textBlock = FindVisualChild <TextBlock>(cell);
                     if (null != textBlock && dragActive)
                     {
                         if (j >= copyInfo.ColumnSourceStartIndex && j <= copyInfo.ColumnSourceEndIndex && i > copyInfo.RowSourceEndIndex && i <= copyInfo.RowTargetEndIndex)
                         {
                             textBlock.Background = Brushes.AliceBlue;
                         }
                         else
                         {
                             textBlock.Background = null;
                         }
                     }
                     else
                     {
                         textBlock.Background = null;
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         string message = ex.Message;
     }
 }
Пример #2
0
        public static void FindDragSource(DataGrid datagrid, IList <DataGridCellInfo> selectedCells, ref CellCopyInfo copyInfo)
        {
            try
            {
                List <object> bindingItems = datagrid.ItemsSource.Cast <object>().ToList();

                foreach (DataGridCellInfo cellInfo in selectedCells)
                {
                    int columnIndex = cellInfo.Column.DisplayIndex;
                    if (copyInfo.ColumnSourceStartIndex == -1 && copyInfo.ColumnSourceEndIndex == -1)
                    {
                        copyInfo.ColumnSourceStartIndex = columnIndex;
                        copyInfo.ColumnSourceEndIndex   = columnIndex;
                    }
                    else if (columnIndex < copyInfo.ColumnSourceStartIndex)
                    {
                        copyInfo.ColumnSourceStartIndex = columnIndex;
                    }
                    else if (columnIndex > copyInfo.ColumnSourceEndIndex)
                    {
                        copyInfo.ColumnSourceEndIndex = columnIndex;
                    }

                    int rowIndex = bindingItems.IndexOf(cellInfo.Item);
                    if (rowIndex > -1)
                    {
                        if (copyInfo.RowSourceStartIndex == -1 && copyInfo.RowSourceEndIndex == -1)
                        {
                            copyInfo.RowSourceStartIndex = rowIndex;
                            copyInfo.RowSourceEndIndex   = rowIndex;
                        }
                        else if (rowIndex < copyInfo.RowSourceStartIndex)
                        {
                            copyInfo.RowSourceStartIndex = rowIndex;
                        }
                        else if (rowIndex > copyInfo.RowSourceEndIndex)
                        {
                            copyInfo.RowSourceEndIndex = rowIndex;
                        }
                    }
                }

                copyInfo.MultipleCells = (copyInfo.RowSourceStartIndex != copyInfo.RowSourceEndIndex) ? true : false;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }
Пример #3
0
        private static Dictionary <int /*columnIndex*/, double /*step*/> FindNumericStep(DataGrid datagrid, CellCopyInfo copyInfo)
        {
            Dictionary <int, double> steps = new Dictionary <int, double>();

            try
            {
                List <object> bindingItems = datagrid.ItemsSource.Cast <object>().ToList();
                List <string> columnNames  = (from column in datagrid.Columns select column.Header.ToString()).ToList();

                if (copyInfo.MultipleCells)
                {
                    for (int j = copyInfo.ColumnSourceStartIndex; j < copyInfo.ColumnSourceEndIndex + 1; j++)
                    {
                        double step = 0;
                        for (int i = copyInfo.RowSourceStartIndex; i < copyInfo.RowSourceEndIndex; i++)
                        {
                            object currentValue       = GetPropertyValue(bindingItems[i], columnNames[j]);
                            double currentSuffix      = 0;
                            bool   currentSuffixFound = GetSuffix(currentValue, out currentSuffix);

                            object nextValue       = GetPropertyValue(bindingItems[i + 1], columnNames[j]);
                            double nextSuffix      = 0;
                            bool   nextSuffixFound = GetSuffix(nextValue, out nextSuffix);

                            if (currentSuffixFound && nextSuffixFound)
                            {
                                double tempStep = nextSuffix - currentSuffix;
                                if (step == 0)
                                {
                                    step = tempStep;
                                }
                                else if (step != tempStep)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (step != 0)
                        {
                            steps.Add(j, step);
                        }
                    }
                }
                else
                {
                    for (int j = copyInfo.ColumnSourceStartIndex; j < copyInfo.ColumnSourceEndIndex + 1; j++)
                    {
                        object value       = GetPropertyValue(bindingItems[copyInfo.RowSourceStartIndex], columnNames[j]);
                        double suffix      = 0;
                        bool   suffixFound = GetSuffix(value, out suffix);
                        if (suffixFound)
                        {
                            steps.Add(j, 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
            return(steps);
        }
Пример #4
0
        public static bool CopyRowItems(DataGrid datagrid, CellCopyInfo copyInfo)
        {
            bool copied = false;

            try
            {
                List <object>            bindingItems = datagrid.ItemsSource.Cast <object>().ToList();
                List <string>            columnNames  = (from column in datagrid.Columns select column.Header.ToString()).ToList();
                Dictionary <int, double> numericSteps = FindNumericStep(datagrid, copyInfo);

                int    copiedRow      = 0;
                int    numSource      = copyInfo.RowSourceEndIndex - copyInfo.RowSourceStartIndex + 1;
                object lastSourceItem = bindingItems[copyInfo.RowSourceEndIndex];

                for (int i = copyInfo.RowSourceEndIndex + 1; i < copyInfo.RowTargetEndIndex + 1; i++)
                {
                    if (i == bindingItems.Count)
                    {
                        break;
                    }

                    object targetItem  = bindingItems[i];
                    int    addIndex    = copiedRow % numSource;
                    int    sourceIndex = copyInfo.RowSourceStartIndex + addIndex;
                    object sourceItem  = bindingItems[sourceIndex];

                    for (int j = copyInfo.ColumnSourceStartIndex; j < copyInfo.ColumnSourceEndIndex + 1; j++)
                    {
                        if (j < columnNames.Count)
                        {
                            string columnName = columnNames[j];
                            if (numericSteps.ContainsKey(j))
                            {
                                //numeric increase copy
                                double step        = numericSteps[j];
                                object sourceValue = GetPropertyValue(lastSourceItem, columnName);

                                double sourceSuffix      = 0;
                                bool   sourceSuffixFound = GetSuffix(sourceValue, out sourceSuffix);

                                if (sourceSuffixFound)
                                {
                                    double suffixValue    = sourceSuffix + ((double)(copiedRow + 1) * step);
                                    string targetValue    = sourceValue.ToString().Replace(sourceSuffix.ToString(), suffixValue.ToString());
                                    object convertedValue = Convert.ChangeType(targetValue, sourceValue.GetType());
                                    targetItem = SetPropertyValue(targetItem, columnName, convertedValue);
                                    bool dbUpdated = UpdateDatabaseItem(targetItem, columnName, convertedValue);
                                }
                            }
                            else
                            {
                                //regular copy and paste
                                object sourceValue = GetPropertyValue(sourceItem, columnName);
                                targetItem = SetPropertyValue(targetItem, columnName, sourceValue);
                                bool dbUpdated = UpdateDatabaseItem(targetItem, columnName, sourceValue);
                            }
                        }
                    }

                    bindingItems.RemoveAt(i);
                    bindingItems.Insert(i, targetItem);
                    copiedRow++;
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
            return(copied);
        }