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

  • Home
  • SEARCH
  • 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 6353823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:28:37+00:00 2026-05-24T22:28:37+00:00

Lets say, we have 10 columns in one datagridview. I have 20 rows of

  • 0

Lets say, we have 10 columns in one datagridview. I have 20 rows of data in these 10 columns.

Dynamically, I’m hiding (setting the .Visible property of the column to false) a few columns – say column 1, 2, 4, 5.

Now I want to copy the contents of the columns which are visible (20 rows of data, 6 visible columns – 3, 6, 7, 8, 9, 10) to a new datagridview.

Any suggestions / advise / links?

I’ve researched this forum and could not find a post discussing about copying a subset of columns from one datagridview to another.

thanks.

  • 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-05-24T22:28:38+00:00Added an answer on May 24, 2026 at 10:28 pm

    Try this (you didn’t indicate whether it was ASP.NET, WinForms, etc – this example is based on WinForms). Christian’s suggestion above is in the right direction, but when I tried it out Clone() didn’t copy the values, and I couldn’t see a way to get it to do that.

    // Set up a List<T> to hold the indexes of the visible columns
    List<int> visibleColumns = new List<int>();
    
    foreach (DataGridViewColumn col in dgv1.Columns)
    {
        if (col.Visible)
        {
            dgv2.Columns.Add((DataGridViewColumn)col.Clone());
    
            visibleColumns.Add(col.Index);
        }
    }
    
    // Now add the data from the columns
    // Set a counter for the current row index for the second DataGridView
    int rowIndex = 0;
    
    foreach (DataGridViewRow row in dgv1.Rows)
    {
    
        // Add a new row to the DataGridView
        dgv2.Rows.Add();
    
        // Loop through the visible columns
        for (int i = 0; i < visibleColumns.Count; i++)
        {
            // Use the index of the for loop for the column in the target data grid
            // Use the index value from the List<T> for the cell of the source target data grid
            dgv2.Rows[rowIndex].Cells[i].Value = row.Cells[visibleColumns[i]].Value;
        }
    
        // Increment the rowIndex
        rowIndex++;
    }
    

    I’ll admit this is ugly and rather brute force, but I tested it and it worked. There may be better ways to do it, but this should at least help you some, I hope.

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

Sidebar

Related Questions

Lets say I have 2 columns in my data Grid: Column A: Selected, and
Lets say I have one row with three columns - some buttons on left
Let's say I have a table with two columns, one column for the ID
Lets say I have a table with columns such as: ID Name City State
Lets say I have table with following columns 1. Client - string. 2. Profit
Lets say I have a table with the following columns: Qty, INTEGER SaleDate, DATETIME
Lets say I have table named Place with columns: placeId int not null auto_increment,
Lets say I have a table in MySQL called articles, that has a column
Lets say I have a table with just two columns: name and mood .
lets say I have a table called table1 and it's corresponding columns are col1,

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.