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

前端 未结 8 1494
不思量自难忘°
不思量自难忘° 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: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);
                }
            }
    

提交回复
热议问题