convert generic list to datatable in Asp.Net C#,VB

convert generic list to datatable in Asp.Net C#,VB

Today, We want to share with you convert generic list to datatable in Asp.Net C#,VB.
In this post we will show you Converting Generic List into DataTable, hear for c# – How to convert a list into data table we will give you demo and example for implement.
In this post, we will learn about Converting Generic List into DataTable with an example.

Introduction

In this post, I will explain how you can convert generic list to DataTable. In the below example I am having a list of colors that I will convert to DataTable.

READ :  Angular 6 CRUD Operations with PHP and MySQLi

Example : Convert generic list to DataTable

[php]
private void FillData()
{
List _ColorList = new List(); // List to be converted into DataTable
_ColorList.Add(“Red”);
_ColorList.Add(“White”);
_ColorList.Add(“Black”);
_ColorList.Add(“Green”);
_ColorList.Add(“Blue”);
DataTable _ResultDataTable = ConvertListToDataTable(_ColorList);
}

public static DataTable ConvertListToDataTable(IList p_ColorList)
{
DataTable _DataTable = new DataTable();
if (typeof(T).IsValueType || typeof(T).Equals(typeof(string)))
{
DataColumn _DataColumn = new DataColumn(“ColorList”);
_DataTable.Columns.Add(_DataColumn);

foreach (T item in p_ColorList)
{
DataRow dr = _DataTable.NewRow();
dr[0] = item;
_DataTable.Rows.Add(dr);
}
}
else
{
PropertyInfo[] _PropertyInfo = typeof(T).GetProperties();
foreach (PropertyInfo pi in _PropertyInfo)
{
DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
_DataTable.Columns.Add(dc);
}

for (int item = 0; item < p_ColorList.Count(); item++)
{
DataRow _DataRow = _DataTable.NewRow();
for (int property = 0; property < _PropertyInfo.Length; property++)
{
_DataRow[property] = _PropertyInfo[property].GetValue(p_ColorList[item], null);
}
_DataTable.Rows.Add(_DataRow);
}
}
_DataTable.AcceptChanges();
return _DataTable;
}
[/php]

READ :  Angular ng-repeat reverse Example - reverse filter

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Converting Generic List into DataTable.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

We hope you get an idea about Converting Generic List into DataTable
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

READ :  Angular simple progress bar using bootstrap

We hope This Post can help you…….Good Luck!.

Searches related to : Converting Generic List into DataTable

  • Converting Generic List into DataTable
  • List to Datatable Converter Using C#
  • Convert Generic List to Data Table
  • How to convert generic list to datatable
  • Convert a Generic List to a Datatable
  • C# Convert List to DataTable (DataGridView)
  • How to convert generic list to datatable in Asp.Net C#,VB

Leave a Comment