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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:47:00+00:00 2026-06-09T05:47:00+00:00

I have generic list … I am trying to convert it to a data

  • 0

I have generic list … I am trying to convert it to a data table.

I am using below code..

private DataTable ToDataTable<T>(List<T> items)
{
        var tb = new DataTable(typeof(T).Name);

        PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo prop in props)
        {
            Type t = GetCoreType(prop.PropertyType);
            tb.Columns.Add(prop.Name, t);
        }

        foreach (T item in items)
        {
            var values = new object[props.Length];

            for (int i = 0; i < props.Length; i++)
            {
             values[i] = props[i].GetValue(item, null);

            }

            tb.Rows.Add(values);
        }

        return tb;
}

I need to add another column in that datatable, say StatusText.

There is already a column in that datatable called Status.

If this column is 0 then StatusText should be Active, or if it is 1 then StatusText will be Inactive

Like my code returns below table currently…

Id   Name  Status
------------------
1    Test   1
2    Test1  0
3    Test2  0

But, how can I generate table like below?

Id   Name  Status  StatusText
------------------------------
1    Test   1       InActive
2    Test1  0       Active
3    Test2  0       Active
  • 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-09T05:47:02+00:00Added an answer on June 9, 2026 at 5:47 am

    Try this

    DataTable dt = ToDataTable(your list); // Get DataTable From List
    
    dt.Columns.Add(new Column("StatusText", typeof(string))); // Add New Column StatusText
    
    
    //Iterate All Rows 
    foreach(DataRow row in dt.Rows)
    {
       //Check Status value, and set StatusText accordingly. 
       row["StatusText"] = int.Parse(row["Status"])==1 ? "InActive" : "Active";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Generic List object which hold below table as its own value.
I have a generic linked-list that holds data of type void* I am trying
I have a generic items list class to create a more specific listing using
I have a generic list that I'm removing items out of using List.Remove(Object) .
I have implemented a generic list and I am trying to retrieve the data
I have a Generic List as below public static readonly List<Customer> Customers = new
i want to use below codes. So i have a generic list<MyClass> i think
I am using asp.net 2.0 and C#. I have a generic list, List<EmployeeInfo> empInfoList;
Assume I have generic list L of some type in c#. Then, using linq,
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using

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.