C# Gridview datasource to datatable - c#

I'm trying to convert a gridview datasource to a datatable.
What I have tried so far
dt = (DataTable)GridCanvas.DataSource; // Unable to cast object of type 'System.Collections.Generic.List`1 to type 'System.Data.DataTable'
Also I have tried this
Unable to cast object of type 'System.Collections.Generic.List`1[CRM.Models.Leads]' to type 'System.Windows.Forms.BindingSource'
BindingSource bindingSource = (BindingSource)GridCanvas.DataSource;
dt = (DataTable)bindingSource.DataSource;
And this
'Object reference not set to an instance of an object.'
dt = GridCanvas.DataSource as DataTable;
I'm populating my gridview in the following way
var dispatchLeads = await API.Zelkon.Leads.Dispatch.Leads(Variables.Agent.username);
GridCanvas.DataSource = dispatchLeads;
I'm trying to avoid loop solutions. Hope someone has an idea how to solve this. Thanks!

First get the List<Leads> from GridCanvas as
List<Leads> data=(List<Leads>)GridCanvas.DataSource;
Then Convert the List<Leads> to DataTable as;
DataTable dt=ToDataTable<Leads>(data);
use following methods for conversion.
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Defining type of data column gives proper data table
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
//Setting column names as Property names
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}

Related

C# Sortable DatagridView doesn't cast to object

I use Dapper to bind data to my dataGridView.
To fill the dataGridView I receive IEnumerable but this is not sortable. So I use the following method.
public static DataTable ToDataTable<T>(this IEnumerable<T> collection)
{
DataTable dt = new DataTable("DataTable");
Type t = typeof(T);
PropertyInfo[] pia = t.GetProperties();
//Inspect the properties and create the columns in the DataTable
foreach (PropertyInfo pi in pia)
{
Type ColumnType = pi.PropertyType;
if ((ColumnType.IsGenericType))
{
ColumnType = ColumnType.GetGenericArguments()[0];
}
dt.Columns.Add(pi.Name, ColumnType);
}
//Populate the data table
foreach (T item in collection)
{
DataRow dr = dt.NewRow();
dr.BeginEdit();
foreach (PropertyInfo pi in pia)
{
if (pi.GetValue(item, null) != null)
{
dr[pi.Name] = pi.GetValue(item, null);
}
}
dr.EndEdit();
dt.Rows.Add(dr);
}
return dt;
}
This works so far.
But I want to use the doubleClick to select a single object from the dataGridView.
Until I used the above method ToDataTable I get the object data this way:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
obj = dataGridView1.Rows[e.RowIndex].DataBoundItem as Part;
...
But now the object always return null.
What do I have to do to get furthermore an object from doubleClick dataGridView?

How to use CopyToDataTable() method in LINQ successfully using below example only?

I have been trying something with LINQ to get all the data from a table customers in a data table. I have used SQL to LINQ class here.
SampleDataContext sdc = new SampleDataContext();
DataTable Cust = new DataTable();
// var query = from c in sdc.customers
// select c;
var query = (IEnumerable<DataRow>)sdc.customers.Select(c => c);
Cust = query.CopyToDataTable();
Console.WriteLine(Cust);
Now when I run this, I get an exception:
Unable to cast object of type 'System.Data.Linq.DataQuery1[BasicLearning.customer]' to type 'System.Collections.Generic.IEnumerable1[System.Data.DataRow]'
Why am I getting this exception?
ANS - from my understanding, which may be wrong, the (IEnumerable<DataRow>) casting in above logic won't work because the result set is not an IEnumerable but IQueryable. But I am not sure if I understand this exception correctly.
So, here I am stuck now, on how to get this code running. I want to use CopyToDataTable() method. But Casting is not working.
It would be great if anyone could help me understand what I am doing wrong here and how to correct it.
as #Svyatoslav Danyliv mention you need to convert object to table. there is an extention which dynamically convert object to datatable.
void Main()
{
DataTable Cust = new DataTable();
//var query = from c in sdc.customers
// select c;
var query = Customers.Select(c => c).ConvertClassToDataTable<Customer>().AsEnumerable();
Cust = query.CopyToDataTable();
Console.WriteLine(Cust);
}
public static class Extensions
{
public static DataTable ConvertClassToDataTable<T>(this IEnumerable<T> list)
{
Type type = typeof(T);
var properties = type.GetProperties();
DataTable dataTable = new DataTable();
foreach (PropertyInfo info in properties)
{
dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
}
foreach (T entity in list)
{
object[] values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(entity);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}

convert list (from web service) to datatable c#

I'm calling WCF Service that gives me a list of customers with specified field names in the BAL. I created a method ToDataTable as instructed on many forums (it might be wrong for this instance). I use it to convert the list into a datatable but there is a challenge that I'm facing. The error says 'Cannot implicitly convert type 'System.Data.DataTable to mHotRes.DesktopPresentation.ListFrm.ListType'.
Here is my code for binding data:
private void BindData()
{
try
{
switch (_ListType)
{
case ListType.Customers:
IHotRes res = new MHotServiceProvider().Service;
List<Customer> customer = res.CustomerSaveDataList();
_ListType = ToDataTable(customer); //the problem occurs here
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Here is the code for ToDataTable method:
public static DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
If any more code samples are needed let me know.
Your reflection ToDataTable code is working correctly:
_ListType = ToDataTable(customer); //the problem occurs here
The problem is that the _ListType have different type from DataTable.
You should change the line to
DataTable tbl = ToDataTable(customer);//Your method returns DataTable

How to Convert a LINQ result to DATATABLE?

Is there any way to convert the result of a LINQ expression to a DataTable without stepping through each element?
Credit to this blogger, but I've improved on his algorithm here. Make yourself an extension method:
public static DataTable ToADOTable<T>(this IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// Use reflection to get property names, to create table
// column names
PropertyInfo[] oProps = typeof(T).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
colType = colType.GetGenericArguments()[0];
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
foreach (T rec in varlist)
{
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
dtReturn.Rows.Add(dr);
}
return (dtReturn);
}
Usage:
DataTable dt = query.ToADOTable();
Nope there is no way to create it without stepping through each element. The Linq expression is evaluated when needed so it will step through each row (for matching and selection).
I think you should try using DataTable.Select() (MSDN link) method instead as it returns array of DataRow objects that you can add to new table as follows:
var rows = [ORIGINAL DATA TABLE].Select("id>5");
var dtb=[ORIGINAL DATA TABLE].Clone();
foreach(DataRow r in rows)
{
var newRow = dtb.NewRow();
newRow.ItemArray = r.ItemArray;
dtb.Rows.Add(newRow);//I'm doubtful if you need to call this or not
}

.NET - Convert Generic Collection to DataTable

I am trying to convert a generic collection (List) to a DataTable. I found the following code to help me do this:
// Sorry about indentation
public class CollectionHelper
{
private CollectionHelper()
{
}
// this is the method I have been using
public static DataTable ConvertTo<T>(IList<T> list)
{
DataTable table = CreateTable<T>();
Type entityType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
table.Rows.Add(row);
}
return table;
}
public static DataTable CreateTable<T>()
{
Type entityType = typeof(T);
DataTable table = new DataTable(entityType.Name);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (PropertyDescriptor prop in properties)
{
// HERE IS WHERE THE ERROR IS THROWN FOR NULLABLE TYPES
table.Columns.Add(prop.Name, prop.PropertyType);
}
return table;
}
}
My problem is that when I change one of the properties of MySimpleClass to a nullable type, I get the following error:
DataSet does not support System.Nullable<>.
How can I do this with Nullable properties/fields in my class?
Then presumably you'll need to lift them to the non-nullable form, using Nullable.GetUnderlyingType, and perhaps change a few null values to DbNull.Value...
Change the assignment to be:
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
and when adding the columns to be:
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
prop.PropertyType) ?? prop.PropertyType);
And it works. (?? is the null-coalescing operator; it uses the first operand if it is non-null, else the second operand is evaluated and used)
Well. Since DataSet does not support nullable types, you'd have to check if the property is a generic type, get the generic definition of that type and then get the argument (which is the actual type) using, perhaps, Nullable.GetUnderlyingType. If the value is null, just use DBNull.Value in the DataSet.
If Nullable.GetUnderlyingType() given your prop.PropertyType returns a not-null value, use that as the type of a column. Otherwise, use prop.PropertyType itself.
I know this question is old, but I had the same issue for an extension method I made. Using the response from Marc Gravell, I was able to modify my code. This extension method will handle lists of primitive types, strings, enumerations and objects with primitive properties.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
/// <summary>
/// Converts a List<T> to a DataTable.
/// </summary>
/// <typeparam name="T">The type of the list collection.</typeparam>
/// <param name="list">List instance reference.</param>
/// <returns>A DataTable of the converted list collection.</returns>
public static DataTable ToDataTable<T>(this List<T> list)
{
var entityType = typeof (T);
// Lists of type System.String and System.Enum (which includes enumerations and structs) must be handled differently
// than primitives and custom objects (e.g. an object that is not type System.Object).
if (entityType == typeof (String))
{
var dataTable = new DataTable(entityType.Name);
dataTable.Columns.Add(entityType.Name);
// Iterate through each item in the list. There is only one cell, so use index 0 to set the value.
foreach (T item in list)
{
var row = dataTable.NewRow();
row[0] = item;
dataTable.Rows.Add(row);
}
return dataTable;
}
else if (entityType.BaseType == typeof (Enum))
{
var dataTable = new DataTable(entityType.Name);
dataTable.Columns.Add(entityType.Name);
// Iterate through each item in the list. There is only one cell, so use index 0 to set the value.
foreach (string namedConstant in Enum.GetNames(entityType))
{
var row = dataTable.NewRow();
row[0] = namedConstant;
dataTable.Rows.Add(row);
}
return dataTable;
}
// Check if the type of the list is a primitive type or not. Note that if the type of the list is a custom
// object (e.g. an object that is not type System.Object), the underlying type will be null.
var underlyingType = Nullable.GetUnderlyingType(entityType);
var primitiveTypes = new List<Type>
{
typeof (Byte),
typeof (Char),
typeof (Decimal),
typeof (Double),
typeof (Int16),
typeof (Int32),
typeof (Int64),
typeof (SByte),
typeof (Single),
typeof (UInt16),
typeof (UInt32),
typeof (UInt64),
};
var typeIsPrimitive = primitiveTypes.Contains(underlyingType);
// If the type of the list is a primitive, perform a simple conversion.
// Otherwise, map the object's properties to columns and fill the cells with the properties' values.
if (typeIsPrimitive)
{
var dataTable = new DataTable(underlyingType.Name);
dataTable.Columns.Add(underlyingType.Name);
// Iterate through each item in the list. There is only one cell, so use index 0 to set the value.
foreach (T item in list)
{
var row = dataTable.NewRow();
row[0] = item;
dataTable.Rows.Add(row);
}
return dataTable;
}
else
{
// TODO:
// 1. Convert lists of type System.Object to a data table.
// 2. Handle objects with nested objects (make the column name the name of the object and print "system.object" as the value).
var dataTable = new DataTable(entityType.Name);
var propertyDescriptorCollection = TypeDescriptor.GetProperties(entityType);
// Iterate through each property in the object and add that property name as a new column in the data table.
foreach (PropertyDescriptor propertyDescriptor in propertyDescriptorCollection)
{
// Data tables cannot have nullable columns. The cells can have null values, but the actual columns themselves cannot be nullable.
// Therefore, if the current property type is nullable, use the underlying type (e.g. if the type is a nullable int, use int).
var propertyType = Nullable.GetUnderlyingType(propertyDescriptor.PropertyType) ?? propertyDescriptor.PropertyType;
dataTable.Columns.Add(propertyDescriptor.Name, propertyType);
}
// Iterate through each object in the list adn add a new row in the data table.
// Then iterate through each property in the object and add the property's value to the current cell.
// Once all properties in the current object have been used, add the row to the data table.
foreach (T item in list)
{
var row = dataTable.NewRow();
foreach (PropertyDescriptor propertyDescriptor in propertyDescriptorCollection)
{
var value = propertyDescriptor.GetValue(item);
row[propertyDescriptor.Name] = value ?? DBNull.Value;
}
dataTable.Rows.Add(row);
}
return dataTable;
}
}
Here's a version with some modifications to allow for nulls and '\0' characters without blowing up the DataTable.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Data;
namespace SomeNamespace
{
public static class Extenders
{
public static DataTable ToDataTable<T>(this IEnumerable<T> collection, string tableName)
{
DataTable tbl = ToDataTable(collection);
tbl.TableName = tableName;
return tbl;
}
public static DataTable ToDataTable<T>(this IEnumerable<T> collection)
{
DataTable dt = new DataTable();
Type t = typeof(T);
PropertyInfo[] pia = t.GetProperties();
object temp;
DataRow dr;
for (int i = 0; i < pia.Length; i++ )
{
dt.Columns.Add(pia[i].Name, Nullable.GetUnderlyingType(pia[i].PropertyType) ?? pia[i].PropertyType);
dt.Columns[i].AllowDBNull = true;
}
//Populate the table
foreach (T item in collection)
{
dr = dt.NewRow();
dr.BeginEdit();
for (int i = 0; i < pia.Length; i++)
{
temp = pia[i].GetValue(item, null);
if (temp == null || (temp.GetType().Name == "Char" && ((char)temp).Equals('\0')))
{
dr[pia[i].Name] = (object)DBNull.Value;
}
else
{
dr[pia[i].Name] = temp;
}
}
dr.EndEdit();
dt.Rows.Add(dr);
}
return dt;
}
}
}

Categories

Resources