Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:53:01+00:00 2026-06-13T21:53:01+00:00

I got 2 datgridviews, trying to copy the header and it values to a

  • 0

I got 2 datgridviews, trying to copy the header and it values to a excel sheet. The first loop is working fine for the first datagirdview

for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[7, i] = dataGridView1.Columns[i - 1].HeaderText;
            }



            // storing Each row and column value to excel sheet
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cells[i + 8, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }

This is the second datagridview and I’m trying to copy the header and it’s values.

The first datagridview header values finished at 7nth row cell D7and the row values finished at 8th row cell D8.

I want to put the second datagridview header from E7 of 7nth row and the row values from E8 of 8th row.

//Primary Continuation
        for (int i = 7; i < dataGridView2.Columns.Count + 1; i++)
        {
            worksheet.Cells[7, i] = dataGridView2.Columns[i - 1].HeaderText;
        }



        // storing Each row and column value to excel sheet
        for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView2.Columns.Count; j++)
            {
                worksheet.Cells[i + 8, j + 1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:53:04+00:00Added an answer on June 13, 2026 at 9:53 pm

    So if I understand you correctly, you want to put your two tables next to each other on the Excel sheet? In which case it looks like you’re getting your offsets mixed up in the loops for the second DataGridView. In this loop:

    //Primary Continuation
    for (int i = 7; i < dataGridView2.Columns.Count + 1; i++)
    {
        worksheet.Cells[7, i] = dataGridView2.Columns[i - 1].HeaderText;
    }
    

    Instead of offsetting your count by 7 (which drives the source data location in the grid), you need to offset the destination in the Excel sheet, since the location in the second DataGridView will be the same as the first. You can replicate your first loop but just change the destination slightly. In fact the same will work for your final loop as well.

    It’s also worth noting that your loop to copy the values will miss out the final row. I’m not sure whether this was intentional or not, but I’ve corrected it. Here is how your second set of loops should look.

    //Primary Continuation
    for (int i = 1; i < dataGridView2.Columns.Count + 1; i++)
    {
        worksheet.Cells[7, i + dataGridView2.Columns.Count] = dataGridView2.Columns[i - 1].HeaderText;
    }
    
    // storing Each row and column value to excel sheet
    for (int i = 0; i < dataGridView2.Rows.Count; i++)
    {
        for (int j = 0; j < dataGridView2.Columns.Count; j++)
        {
            worksheet.Cells[i + 8, j + 1 + dataGridView2.Columns.Count] = dataGridView2.Rows[i].Cells[j].Value.ToString();
        }
    }
    

    You’ll notice that since the difference between your two sets of loops is now negligible, you could easily combine them together, cutting down on execution time (although obviously the impact would only be noticeable with much more data).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got this error message while trying to load view: The model item passed into
I am trying to save a dataset to a database. I got a dataset
I got the following error when trying to retrieve the data to the datetime
So basically I've got 2 DataGridView and I need to copy the rows from
I've got my DataGridView binding correctly to my datatable however I'm trying to get
I've got an application with a ListView and I want to loop through each
I am trying to listen on a device and when got a message from
i've got an event handler for columncontentclicked and it works fine, until i shorten
I have been trying to use OleDbDataAdapter to update a DataTable but got confused
I've got a DataGridView in a modal window with a list of options for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.