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 7652499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:47:12+00:00 2026-05-31T11:47:12+00:00

I have a routine which converts all the data in a DataGridView to a

  • 0

I have a routine which converts all the data in a DataGridView to a corresponding DataTable. I want to extend this method to incorporate the ability to convert just the selected range of the DataGridView. I can clearly do this using basic logic if (bIsSelection) /*Do stuff*/ else /*Do other stuff*/ but I would like to use generics here. The problem is that the full DataGridView range is a DataGridViewColumnCollection and the selected range will be a DataGridViewSelectedColumnCollection and C# does not seem to like any conversion between the two, or allow implicit typing in the case of generics.

The first part of my code was

public static DataTable BuildDataSetFromDgv(DataGridView _dataGridView, 
    string strTabName)
{
    DataTable dt = new DataTable();

    dt.TableName = strTabName;
    foreach (DataGridViewColumn col in _dataGridView.Columns)
        dt.Columns.Add(col.DataPropertyName, col.ValueType);

and I have attempted

public static DataTable BuildDataSetFromDgv<T>(DataGridView _dataGridView, 
    string strTabName, ICollection<T> _columnColl, ICollection<T> _rowColl)
{
    DataTable dt = new DataTable();

    dt.TableName = strTabName;
    //foreach (DataGridViewColumn col in _dataGridView.Columns)
    foreach (DataGridViewColumn col in _columnColl)
        dt.Columns.Add(col.DataPropertyName, col.ValueType);

which does not work due to the implicit conversion, and changing _columnColl to _columnColl as DataGridView[Selected]ColumnCollection defeats the objective.

How do I make this work with generics in this case?

Thanks for your time.

  • 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-31T11:47:13+00:00Added an answer on May 31, 2026 at 11:47 am

    I am not sure why you need a generic one – both are collection of DataGridViewColumn. For example, why below shouldn’t work for you

    public static DataTable BuildDataSetFromDgv(DataGridView _dataGridView, 
        string strTabName, IEnumerable columns)
    {
        DataTable dt = new DataTable();
        dt.TableName = strTabName;
    
        var dvgColumns = columns.Cast<DataGridViewColumn>();
        foreach (var col in dvgColumns)
            dt.Columns.Add(col.DataPropertyName, col.ValueType);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this routine which is called onmouseover of a certain element. I want
I have this routine which alters all elements within an array... for (int i
I have a routine which is updating my business entity. The update involves about
I have a routine which uses a recursive loop to insert items into a
I have this routine that calculates the seconds-to-date for a struct tm . On
I have a routine that examines thousands of records looking for discrepancies. This can
I have a short routine which arranges ggplot and grid graphics using the grid.arrange
I have a routine which reads in an image, resizes it and positions it
I have a simple routine which calculates the aspect ratio from a floating point
I populate the data from my table using a loadResults routine which runs on

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.