Pasting excel data into a blank DataGridView - Index out of range exception

前端 未结 8 1453
不思量自难忘°
不思量自难忘° 2020-12-30 07:20

I have an excel sheet with the following:

\"enter

So, what I am trying to achi

相关标签:
8条回答
  • 2020-12-30 08:08

    Have your gridview columns defined, if not you have to define the columns first.

    private void PasteClipboard(DataGridView myDataGridView)
    {
        DataObject o = (DataObject)Clipboard.GetDataObject();
        if (o.GetDataPresent(DataFormats.Text))
        {
            string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
            foreach (string pastedRow in pastedRows)
            {
                string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
                using (DataGridViewRow myDataGridViewRow = new DataGridViewRow())
                {
                    myDataGridViewRow = (DataGridViewRow) myDataGridView.RowTemplate.Clone();
                    for (int i = 0; i < pastedRowCells.Length; i++)
                        myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
    
                    myDataGridView.Rows.Add(myDataGridViewRow);
                }
            }
        }
    }
    

    IF not definced columns

    private void PasteClipboard(DataGridView myDataGridView)
    {
        //Create COlumns in datagridView
        myDataGridView = new DataGridView();
        myDataGridView.Columns.Add("col1", "Col1");
        myDataGridView.Columns.Add("col2", "Col2");
        myDataGridView.Columns.Add("col3", "Col3");
        myDataGridView.Columns.Add("col4", "Col4");
    
        DataObject o = (DataObject)Clipboard.GetDataObject();
        if (o.GetDataPresent(DataFormats.Text))
        {
            string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
            foreach (string pastedRow in pastedRows)
            {
                string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
                using (DataGridViewRow myDataGridViewRow = new DataGridViewRow())
                {
                    myDataGridViewRow = (DataGridViewRow) myDataGridView.RowTemplate.Clone();
                    for (int i = 0; i < pastedRowCells.Length; i++)
                        myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
    
                    myDataGridView.Rows.Add(myDataGridViewRow);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 08:15

    In case you are dealing with Unicode here is the code to paste to a DataTable that is binded to a the DataGridView

            DataObject o = (DataObject)Clipboard.GetDataObject();
            if (o.GetDataPresent(DataFormats.Text))
            {
                string[] pastedRows = Regex.Split(o.GetText().TrimEnd("\r\n".ToCharArray()), "\r\n");
                foreach (string pastedRow in pastedRows)
                {
                    string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
                    var temp = dt1.NewRow();
                    for (int i = 0; i < pastedRowCells.Length; i++)
                        temp[i] = pastedRowCells[i];
                    dt1.Rows.Add(temp);
                }
            }
    
    0 讨论(0)
提交回复
热议问题