locked
C# Copy one Datagrid Row to another Row, Excel import RRS feed

  • Question

  • Trying to copy one datagrid row to another row, below code copies a hard coded number to another row. I need to copy say cell 7 row to cell 10 row.

     private void btn_Adjust_Click(object sender, EventArgs e)
    
            
    for (int i = 0; i < dataGridView1.RowCount; i++)
                {

                    if (dataGridView1.Rows[i].Cells[18].Value.ToString() == "Yeshh")
                {
                    dataGridView1.Rows[i].Cells[9].Value = "48";
                    dataGridView1.Rows[i].Cells[8].Value = "45";



                        var value3 = dataGridView1.Rows[i].Cells[7].Value.ToString();
                        dataGridView1.Rows[i].Cells[10].Value = value3;
                        var K = value3;
                         dataGridView1.Rows[i].Cells[10].Value = K;


    Booney440


    • Edited by Booney440 Saturday, November 24, 2018 4:01 PM update
    Saturday, November 24, 2018 2:36 AM

Answers

  • Hi,

    >> below code copies a hard coded number to another row

    The code you provided is to copy one column of data to another column instead of the row.

    >>I need to copy say cell 7 row to cell 10 row.

    Try this:

        private void btn_Adjust_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Rows[9].Cells[i].Value = dataGridView1.Rows[6].Cells[i].Value;
            }
        }

    Regards,

    Stanly


    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.

    • Marked as answer by Booney440 Monday, November 26, 2018 9:56 PM
    Monday, November 26, 2018 6:21 AM

All replies

  • Hi,

    >> below code copies a hard coded number to another row

    The code you provided is to copy one column of data to another column instead of the row.

    >>I need to copy say cell 7 row to cell 10 row.

    Try this:

        private void btn_Adjust_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Rows[9].Cells[i].Value = dataGridView1.Rows[6].Cells[i].Value;
            }
        }

    Regards,

    Stanly


    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.

    • Marked as answer by Booney440 Monday, November 26, 2018 9:56 PM
    Monday, November 26, 2018 6:21 AM
  • Stanly Fan

    Thank You


    Booney440

    Tuesday, November 27, 2018 11:21 PM