0

mi duda surge ya que intento pegar los datos de "n" columnas y "n" filas. Puesto que estoy intentado con ClipBoard pero no me pega la información que tengo en el portapales.

private void PegarBtn_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            var text = Clipboard.GetData(DataFormats.Text) as string;

            if (string.IsNullOrWhiteSpace(text))
                return;

            string[] lines = text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Paste failed", MessageBoxButton.OK);
        }
    }
2
  • No está muy clara tu pregunta. En tu código lo unico que haces es crear un array con las filas del texto que tengas en el Clipboard, siempre y cuando estén separadas por un retorno de carro. Estás seguro de que es así como estan separadas?
    – Pikoh
    el 9 nov. 2017 a las 16:36
  • Necesito hacer una pantalla que contenga un combobox, un boton que pegara la información copiada de excel, un grid y un Datatemplate para mostrar la información que se pegue en el grid(como un resumen)
    – Ab Hdez
    el 9 nov. 2017 a las 17:12

1 respuesta 1

0

Te falta la parte donde se realiza el "pegado". Te dejo el código completo que encontré :

private void PasteClipboard()
{
    try
    {
        string s = Clipboard.GetText();
        string[] lines = s.Split('\n');
        int iFail = 0, iRow = dgData.CurrentCell.RowIndex;
        int iCol = dgData.CurrentCell.ColumnIndex;
        DataGridViewCell oCell;
        foreach (string line in lines)
        {
            if (iRow < dgData.RowCount && line.Length > 0)
            {
                string[] sCells = line.Split('\t');
                for (int i = 0; i < sCells.GetLength(0); ++i)
                {
                    if (iCol + i < this.dgData.ColumnCount)
                    {
                        oCell = dgData[iCol + i, iRow];
                        if (!oCell.ReadOnly)
                        {
                            if (oCell.Value.ToString() != sCells[i])
                            {
                                oCell.Value = Convert.ChangeType(sCells[i], 
                                                      oCell.ValueType);
                                oCell.Style.BackColor = Color.Tomato;
                            }
                            else
                                iFail++;
                                //only traps a fail if the data has changed 
                                //and you are pasting into a read only cell
                        }
                    }
                    else
                    { break; }
                }
                iRow++;
            }
            else
            { break; }
            if (iFail > 0)
                MessageBox.Show(string.Format("{0} updates failed due" + 
                                " to read only column setting", iFail));
        }
    }
    catch (FormatException )
    {
        MessageBox.Show("The data you pasted is in the wrong format for the cell");
        return;
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.