Ejemplo n.º 1
0
    //</snippet05>

    //<snippet10>
    private void selectedCellsButton_Click(object sender, System.EventArgs e)
    {
        Int32 selectedCellCount =
            dataGridView1.GetCellCount(DataGridViewElementStates.Selected);

        if (selectedCellCount > 0)
        {
            if (dataGridView1.AreAllCellsSelected(true))
            {
                MessageBox.Show("All cells are selected", "Selected Cells");
            }
            else
            {
                System.Text.StringBuilder sb =
                    new System.Text.StringBuilder();

                for (int i = 0;
                     i < selectedCellCount; i++)
                {
                    sb.Append("Row: ");
                    sb.Append(dataGridView1.SelectedCells[i].RowIndex
                              .ToString());
                    sb.Append(", Column: ");
                    sb.Append(dataGridView1.SelectedCells[i].ColumnIndex
                              .ToString());
                    sb.Append(Environment.NewLine);
                }

                sb.Append("Total: " + selectedCellCount.ToString());
                MessageBox.Show(sb.ToString(), "Selected Cells");
            }
        }
    }
Ejemplo n.º 2
0
 /// <summary>
 ///   Copies the selected Cells into clipboard.
 /// </summary>
 /// <param name="dataGridView">The data grid view.</param>
 /// <param name="addErrorInfo">if set to <c>true</c> add error information.</param>
 /// <param name="cutLength">if set to <c>true</c> cut off long text.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 public void SelectedDataIntoClipboard(
     [NotNull] DataGridView dataGridView,
     bool addErrorInfo,
     bool cutLength,
     CancellationToken cancellationToken)
 {
     dataGridView.RunWithHourglass(() =>
     {
         if (dataGridView.GetCellCount(DataGridViewElementStates.Selected) == 1)
         {
             CopyOneCellIntoClipboard(dataGridView.SelectedCells[0]);
         }
         else if (dataGridView.AreAllCellsSelected(false))
         {
             CopyAllCellsIntoClipboard(
                 addErrorInfo,
                 cutLength,
                 !Equals(dataGridView.AlternatingRowsDefaultCellStyle, dataGridView.RowsDefaultCellStyle),
                 dataGridView.Columns,
                 dataGridView.Rows,
                 cancellationToken);
         }
         else
         {
             CopySelectedCellsIntoClipboard(
                 addErrorInfo,
                 cutLength,
                 !Equals(dataGridView.AlternatingRowsDefaultCellStyle, dataGridView.RowsDefaultCellStyle),
                 dataGridView.Columns,
                 dataGridView.Rows,
                 dataGridView.SelectedCells,
                 cancellationToken);
         }
     });
 }
Ejemplo n.º 3
0
        /// <summary>
        ///   Copies the selected Cells into clipboard.
        /// </summary>
        /// <param name="dataGridView">The data grid view.</param>
        /// <param name="addErrorInfo">if set to <c>true</c> add error information.</param>
        /// <param name="cutLength">if set to <c>true</c> cut off long text.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public static void SelectedDataIntoClipboard(this DataGridView dataGridView,
                                                     bool addErrorInfo,
                                                     bool cutLength,
                                                     CancellationToken cancellationToken)
        {
            var oldCursor = Cursor.Current == Cursors.WaitCursor ? Cursors.WaitCursor : Cursors.Default;

            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (dataGridView.GetCellCount(DataGridViewElementStates.Selected) == 1)
                {
                    CopyOneCellIntoClipboard(dataGridView.SelectedCells[0]);
                }
                else if (dataGridView.AreAllCellsSelected(false))
                {
                    CopyAllCellsIntoClipboard(addErrorInfo, cutLength,
                                              !Equals(dataGridView.AlternatingRowsDefaultCellStyle, dataGridView.RowsDefaultCellStyle),
                                              dataGridView.Columns,
                                              dataGridView.Rows, cancellationToken);
                }
                else
                {
                    CopySelectedCellsIntoClipboard(addErrorInfo, cutLength,
                                                   !Equals(dataGridView.AlternatingRowsDefaultCellStyle, dataGridView.RowsDefaultCellStyle),
                                                   dataGridView.Columns,
                                                   dataGridView.Rows, dataGridView.SelectedCells, cancellationToken);
                }
            }
            catch (Exception exc)
            {
                dataGridView.FindForm().ShowError(exc, "Error filling clipboard");
            }
            finally
            {
                Cursor.Current = oldCursor;
            }
        }
Ejemplo n.º 4
0
        private static int[] ColumnsSelected(DataGridView dataGridView1)
        {
            int counts            = 0;
            int UseToIdentify     = 0;
            int countforNew       = 0;
            int selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);

            if (selectedCellCount > 0)
            {
                int[] ColumnsChosen = new int[selectedCellCount];
                for (int i = 0; i < selectedCellCount; i++)
                {
                    ColumnsChosen[i] = -100;
                }
                if (dataGridView1.AreAllCellsSelected(true))
                {
                    for (int i = 0; i < dataGridView1.Columns.Count; i++)
                    {
                        counts = 0;
                        foreach (int EachCol in ColumnsChosen)
                        {
                            if (EachCol == i)
                            {
                                counts++;
                                break;
                            }
                        }
                        if (counts == 0)
                        {
                            ColumnsChosen[i] = i;
                        }
                        else
                        {
                            ColumnsChosen[i] = -100;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        UseToIdentify = dataGridView1.SelectedCells[i].ColumnIndex;
                        counts        = 0;
                        foreach (int EachCol in ColumnsChosen)
                        {
                            if (EachCol == UseToIdentify)
                            {
                                counts++;
                                break;
                            }
                        }
                        if (counts == 0)
                        {
                            ColumnsChosen[countforNew] = UseToIdentify;
                            countforNew++;
                        }
                        else
                        {
                            ColumnsChosen[countforNew] = -100;
                            countforNew++;
                        }
                    }
                }
                return(ColumnsChosen);
            }
            return(new int[] { -1, -1, -1, -1 });
        }