Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

I want to put the grid data in Excel when I insert it, no matter where I copy the Excel

$
0
0
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 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, 1, 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);
                }

            }
        }

I'm pasting it  from Excel to Gridview, but the data isn't working properly.I want to paste data from any cell.   Colum number =11



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>