none
After copying the data from excel and pasting it into the datagridview, only the empty value is pasted. Help me.

    Question

  •  private string ClipboardData
            {
                get
                {
                    IDataObject iData = Clipboard.GetDataObject();
                    if (iData == null) return '';
    
                    if (iData.GetDataPresent(DataFormats.Text))
                        return (string)iData.GetData(DataFormats.Text);
                    return '';
                }
                set { Clipboard.SetDataObject(value); }
            }
    
            private void CopyRowAdd(string data, int idx)
            {
                try
                {
                    DataTable tblData = (DataTable)grdXYMappingInfo.DataSource;
                    DataRow newRow = tblData.NewRow();
    
                    string[] rawRowData = data.Split(new char[] { '\r', '\x09' });
                    string[] RowData = new string[tblData.Columns.Count];
    
                    for (int i = 0; i <  RowData.Length; i++)
                    {
                        if (i >= tblData.Columns.Count) break;
                        else
                        {
                            newRow[i] = RowData[i];
                        }
                    }
                    tblData.Rows.InsertAt(newRow, grvXYMappingInfo.FocusedRowHandle + idx);
                }
                catch (Exception x)
                {
                    System.Console.WriteLine(x.Message);
                }
            }
    
            private void grvXYMappingInfo_KeyDown(object sender, KeyEventArgs e)
            {
    
                if (e.Control && e.KeyCode == Keys.C)
                {
    
                    grvXYMappingInfo.CopyToClipboard();
    
                }
                else if (e.Control && e.KeyCode == Keys.V)
                {
                    try
                    {
    
                        string[] selectedRow = ClipboardData.Split('\n');
                        string[] addRow = new string[selectedRow.Length - 1];
                        System.Array.Copy(selectedRow, 1, addRow, 0, selectedRow.Length - 1);
    
                        if (addRow.Length <  1) return;
                        int rowidx = 1;
                        foreach (string row in addRow)
                        {
                            CopyRowAdd(row, rowidx);
                            rowidx++;
                        }
                    }
                    catch (Exception x)
                    {
                        System.Console.WriteLine(x.Message);
                    }
    
                }
            }
    After copying the data from Excel and pasting it into the datagridview, only the empty value is pasted. Help me.

    Thursday, May 23, 2019 3:12 AM

All replies

  • Maybe replace ‘newRow[i] = RowData[i]’ with ‘newRow[i] = rawRowData[i]’.

    Thursday, May 23, 2019 5:57 AM
  • Hi chh.522,

    Thank you for posting here.

    Based on your description, you want to copy the data from the excel and paste it into datagridview.

    You could try the following code, the bold code is modified or added.

     private void CopyRowAdd(string data, int idx)
            {
                try
                {
                    DataTable tblData = (DataTable)dataGridView1.DataSource;
                    DataRow newRow = tblData.NewRow();
    
                    string[] rawRowData = data.Split(new char[] { '\r', '\x09' });
    
                    for (int i = 0; i < rawRowData.Length; i++)
                    {
                        if (i >= tblData.Columns.Count) break;
                        else
                        {
                            newRow[i] = rawRowData[i];
                        }
                    }
                    tblData.Rows.Add(newRow);
                    dataGridView1.DataSource = tblData;
                }
                catch (Exception x)
                {
                    System.Console.WriteLine(x.Message);
                }
            }
        
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add("A", typeof(string));
                table.Columns.Add("B", typeof(string));
                table.Columns.Add("C", typeof(string));
                dataGridView1.ReadOnly = true;
                dataGridView1.DataSource = table;
    
    
            }
    
            private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
       
            {
                
    
                if (e.Control && e.KeyCode == Keys.C)
                {
    
                    grvXYMappingInfo.CopyToClipboard();
    
                }
                else           //
                  
                     if ( e.Control&&e.KeyCode == Keys.V)
                {
                    try
                    {
    
                        string[] selectedRow = ClipboardData.Split('\n');
                        string[] addRow = new string[selectedRow.Length-1];
                        System.Array.Copy(selectedRow, 0, addRow, 0, selectedRow.Length - 1);
    
                        if (addRow.Length < 1) return;
                        int rowidx = 1;
                        foreach (string row in addRow)
                        {
                            CopyRowAdd(row, rowidx);
                            rowidx++;
                        }
                        MessageBox.Show("success");
                    }
                    catch (Exception x)
                    {
                        System.Console.WriteLine(x.Message);
                    }
    
                }
            }

    Result:

    .

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, May 23, 2019 6:38 AM
  • Hello,

    Give this a try.

    using System;
    using System.Data;
    using System.IO;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void cmdRun_Click(object sender, EventArgs e)
            {
                dataGridView1.DataSource = null;
                try
                {
                    var clipboardData = Clipboard.GetDataObject();
    
                    if (clipboardData != null)
                    {
                        if (clipboardData.GetDataPresent(DataFormats.CommaSeparatedValue))
                        {
    
                            var clipboardStream =
                                new StreamReader((Stream) (clipboardData.GetData(DataFormats.CommaSeparatedValue)));
    
                            string formattedData = "";
                            var table = new DataTable { TableName = "ExcelData" };
    
                            while (clipboardStream.Peek() > 0)
                            {
                                Array singleRowData = null;
                                int loopCounter = 0;
    
                                formattedData = clipboardStream.ReadLine();
    
                                if (formattedData != null) singleRowData = formattedData.Split(",".ToCharArray());
    
                                if (table.Columns.Count <= 0)
                                {
                                    for (loopCounter = 0; loopCounter <= singleRowData.GetUpperBound(0); loopCounter++)
                                    {
                                        table.Columns.Add();
                                    }
                                    loopCounter = 0;
                                }
    
                                DataRow rowNew = null;
                                rowNew = table.NewRow();
    
                                for (loopCounter = 0; loopCounter <= singleRowData.GetUpperBound(0); loopCounter++)
                                {
                                    rowNew[loopCounter] = singleRowData.GetValue(loopCounter);
                                }
    
                                loopCounter = 0;
    
                                table.Rows.Add(rowNew);
    
                                rowNew = null;
                            }
    
                            clipboardStream.Close();
                            dataGridView1.DataSource = table;
                        }
                        else
                        {
                            MessageBox.Show("Clipboard data does not seem to be copied from Excel!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Clipboard is empty!");
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 23, 2019 10:23 AM
    Moderator
  • I want to make a copy to the column in any cell, but not now.
    Friday, May 24, 2019 1:39 AM
  • Hi

    Thanks for the feedback.

    I want to confirm if you have tried the solution that I provided. If you have any question about my solution, please feel free to let me know.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 24, 2019 1:44 AM
  • I want to put the grid data in Excel when I insert it, no matter where I copy the Excel 

    I write thread !!

    Friday, May 24, 2019 2:35 AM
  • I want to put the grid data in Excel when I insert it, no matter where I copy the Excel 

    thread
    • Edited by chh.552 Friday, May 24, 2019 3:18 AM
    Friday, May 24, 2019 3:17 AM