C# complex class to SQLBulkInsert millions or rows - c#

I'm having an issue, apparently is very simple but I haven't found anything related.
public class Fruit
{
public string name { get; set; }
public string color { get; set; }
public string shape { get; set; }
public Image image { get; set; }
}
public class Image
{
public string Id { get; set; }
public string URL { get; set; }
}
That's my "complex" class, and I want to convert this to DataTable to apply the SQLBulkCopy but when it retrieves the Image class it's not bringing the values but the data type "Image" which throws an exception.
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
Any clue on how can I retrieve the ID for the Image property to do the SQLBulkCopy?
By the way, this is the method I'm using for the insert:
public static void SaveToDatabase(DataTable data)
{
using (var connection = new SqlConnection(dbConn))
{
SqlTransaction transaction = null;
connection.Open();
try
{
transaction = connection.BeginTransaction();
using (var sqlBulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, transaction))
{
sqlBulkCopy.DestinationTableName = "Fruit";
sqlBulkCopy.ColumnMappings.Add("name", "name");
sqlBulkCopy.ColumnMappings.Add("color", "color");
sqlBulkCopy.ColumnMappings.Add("shape", "shape");
sqlBulkCopy.ColumnMappings.Add("image", "image");
sqlBulkCopy.WriteToServer(data);
}
transaction.Commit();
}
catch (Exception ex) // Here I got the datatype Exception because I don't know how to get the Image.ID value
{
transaction.Rollback();
}
}
}
Apparently my post it's a little bit confusing, so I'm going to add the issue here:
How to get the URL and ID values from the Image class in the following code? Currently, it's bringing the object Image which it's unknown and throws an exception
sqlBulkCopy.ColumnMappings.Add("image", "image");
How can I map them? I know I can do it row by row, but I have more than 2 million rows to process on each execution. That's why I'm trying to use the bulkcopy.
I need to add that this data comes from an API call in Json format

After modifying the code to test this I did find a solution:
if (prop.GetChildProperties().Count > 1)
row[prop.Name] = (Image)prop.GetValue(item);
else
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
The solution was only to cast the GetValue to the Image class, this was applied in the ToDataTable method:
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
if (prop.GetChildProperties().Count > 1)
row[prop.Name] = (Image)prop.GetValue(item);
else
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
Thank you all for your time and help!

Related

How do I use reflection to dynamically call a type?

I have a DataGrid and I need to convert it to a DataTable. The problem is that I need To be able to get the type of my DataSource dynamically. The grid DataSource is of type 'Observable' which is a class I have in my project, but my task is to be able to dynamically create the DataTable without needing to specify the type only using the DataSource. How can I generate a method that I can use to place in <T> without getting the error " 'mytype' is a variable but is used like a type" .
Type mytype = Grid_Job.DataSource.GetType();
DataTable dt = CreateDataTable<mytype>((IEnumerable<mytype>)Grid_Job.DataSource);
public static DataTable CreateDataTable<T>(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;
}
My Observable class is
public class Observable
{
public int JobNo { get; set; }
public string JobName { get; set; }
public string JobDescription { get; set; }
public string Job_Type { get; set; }
public string Job_Status { get; set; }
}
Effectively, you need to discard the generics and think in terms of the collection at runtime - discovering the element type itself. For example:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Reflection;
static class P
{
static object GetData()
=> new ObservableCollection<Observable>
{
new Observable { JobName = "abc", JobNo = 123 },
new Observable { JobName = "def", JobNo = 456 },
new Observable { JobName = "ghi", JobNo = 789 },
};
static void Main()
{
// note that we don't know *anything* about the data source here
object dataSource = GetData();
DataTable dt = CreateDataTable((IEnumerable)dataSource);
foreach (DataColumn col in dt.Columns)
{
Console.Write(col.ColumnName);
Console.Write("\t");
}
Console.WriteLine();
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
{
Console.Write(row[col]);
Console.Write("\t");
}
Console.WriteLine();
}
}
public static DataTable CreateDataTable(IEnumerable list)
{
Type type = GetElementType(list.GetType());
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));
}
object[] values = new object[properties.Length];
foreach (object entity in list)
{
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(entity);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
static Type GetElementType(Type type)
{
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType.IsGenericType &&
interfaceType.GetGenericTypeDefinition()
== typeof(IList<>))
{
return type.GetGenericArguments()[0];
}
}
return null;
}
}
public class Observable
{
public int JobNo { get; set; }
public string JobName { get; set; }
public string JobDescription { get; set; }
public string Job_Type { get; set; }
public string Job_Status { get; set; }
}
Note that in some cases you should prefer the TypeDescriptor model over reflection, as this allows runtime definition of properties; this is a niche area and probably doesn't apply to you, but it relevant to know about. As an example, this is how DataTable itself chooses to expose the properties it has as discoverable at runtime to general purpose tools. There are also a few list indirection APIs that need to be considered in those cases.

C# Cannot cast List to DataTable

I'm having some problems to export a DataGridView to Excel.
So, after user choose some filters, the data is passed to another form and the user has the option to export to excel, but there's an error saying "Cannot cast object List to DataTable.
Let me show you the code...
Query to List --> Working fine
public class Users
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public IEnumerable<Users> LoadUsers()
{
var ctx = new Entities();
var query = (from p in ctx.tblUser.AsQueryable()
join c in ctx.tblNames on p.NameID equals c.NameID
join e in ctx.tblAddresses on p.AddressID equals e.AddressID
select new Users
{
ID = p.ProjetoID,
Name = c.Name,
Address = e.Address
});
return query.ToList();
}
Button Click Event
private void BtnSearch_Click(object sender, EventArgs e)
{
if(LoadUsers() == null)
{
MessageBox.Show("No results!", "No results", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
UsersSearch_List searchlist = new UsersSearch_List();
searchlist.users_datagrid.DataSource = LoadUsers();
searchlist.MdiParent = MdiParent;
searchlist.Show();
}
}
On the other Form, the data loads OK in users_datagrid DataGridView. But, when User clicks on Export Button:
private void exportToolStripMenuItem_Click(object sender, EventArgs ev)
{
DataTable dt = new DataTable();
dt = (DataTable)users_datagrid.DataSource; //--> The error occurs here... Cannot cast object List to DataTable.
//This is a Helper I have to convert List to DataTable, but it don't works eather!!!
//DataTable dt = ConvertListToDataTable.ToDataTable(projetos_datagrid);
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
string TargetFileName = saveFileDialog.FileName;
try
{
CreateExcelFile.CreateExcelDocument(dt, TargetFileName);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
return;
}
Process process = new Process();
process.StartInfo = new ProcessStartInfo(TargetFileName);
process.Start();
}
Can someone help me???
You should create a new DataTable and populate it using the list:
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}

Error while converting List of objects to Datatable

Hello I have the following method.
public static DataTable ConvertToDataTable<T>(List<T> lstData)
{
PropertyDescriptorCollection objPropertiesCollection = TypeDescriptor.GetProperties(typeof(T));
DataTable dtResult = new DataTable();
foreach (PropertyDescriptor objProperty in objPropertiesCollection)
{
dtResult.Columns.Add(objProperty.Name, Nullable.GetUnderlyingType(objProperty.PropertyType) ?? objProperty.PropertyType);
}
foreach (T item in lstData)
{
DataRow dr = dtResult.NewRow();
foreach (PropertyDescriptor objProperty in objPropertiesCollection)
{
dr[objProperty.Name] = objProperty.GetValue(item) ?? DBNull.Value;
}
dtResult.Rows.Add(dr);
}
return dtResult;
}
and I have the following Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ClientesPagos.Clases
{
[Serializable]
public class Servicio
{
public int IdServicio;
public string Nombre;
public string Descripcion;
public string Imagen;
public decimal Precio;
public int Cantidad;
public decimal Total;
public Servicio()
{
}
public Servicio(int id, string nombre, string descripcion, string img, decimal precio, int cantidad)
{
IdServicio = id;
Nombre = nombre;
Descripcion = descripcion;
Imagen = img;
Precio = precio;
Cantidad = cantidad;
Total = cantidad * precio;
}
}
}
now the problem is on the first line of the method
PropertyDescriptorCollection objPropertiesCollection =
TypeDescriptor.GetProperties(typeof(T));
It does not fill objPropertiesCollection and therefore the following instructions don't work.
Is there something wrong on the class, that it may not be working on the first Method?
You are trying to get properties an you should change your fields to properties.
Those are properties:
public int IdServicio {get; set;}
public string Nombre { get; set; }
public string Descripcion { get; set; }
public string Imagen { get; set; }
public decimal Precio { get; set; }
public int Cantidad { get; set; }
public decimal Total { get; set; }
So you want to convert "everything" to a DataTable? Then i would use the CopyToDataTable approach on MSDN with it's ObjectShredder class.
How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
public class ObjectShredder<T> {
private System.Reflection.FieldInfo[] _fi;
private System.Reflection.PropertyInfo[] _pi;
private System.Collections.Generic.Dictionary<string, int> _ordinalMap;
private System.Type _type;
// ObjectShredder constructor.
public ObjectShredder() {
_type = typeof(T);
_fi = _type.GetFields();
_pi = _type.GetProperties();
_ordinalMap = new Dictionary<string, int>();
}
/// <summary>
/// Loads a DataTable from a sequence of objects.
/// </summary>
/// <param name="source">The sequence of objects to load into the DataTable.</param>
/// <param name="table">The input table. The schema of the table must match that
/// the type T. If the table is null, a new table is created with a schema
/// created from the public properties and fields of the type T.</param>
/// <param name="options">Specifies how values from the source sequence will be applied to
/// existing rows in the table.</param>
/// <returns>A DataTable created from the source sequence.</returns>
public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption? options) {
// Load the table from the scalar sequence if T is a primitive type.
if (typeof(T).IsPrimitive) {
return ShredPrimitive(source, table, options);
}
// Create a new table if the input table is null.
if (table == null) {
table = new DataTable(typeof(T).Name);
}
// Initialize the ordinal map and extend the table schema based on type T.
table = ExtendTable(table, typeof(T));
// Enumerate the source sequence and load the object values into rows.
table.BeginLoadData();
using (IEnumerator<T> e = source.GetEnumerator()) {
while (e.MoveNext()) {
if (options != null) {
table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options);
} else {
table.LoadDataRow(ShredObject(table, e.Current), true);
}
}
}
table.EndLoadData();
// Return the table.
return table;
}
public DataTable ShredPrimitive(IEnumerable<T> source, DataTable table, LoadOption? options) {
// Create a new table if the input table is null.
if (table == null) {
table = new DataTable(typeof(T).Name);
}
if (!table.Columns.Contains("Value")) {
table.Columns.Add("Value", typeof(T));
}
// Enumerate the source sequence and load the scalar values into rows.
table.BeginLoadData();
using (IEnumerator<T> e = source.GetEnumerator()) {
Object[] values = new object[table.Columns.Count];
while (e.MoveNext()) {
values[table.Columns["Value"].Ordinal] = e.Current;
if (options != null) {
table.LoadDataRow(values, (LoadOption)options);
} else {
table.LoadDataRow(values, true);
}
}
}
table.EndLoadData();
// Return the table.
return table;
}
public object[] ShredObject(DataTable table, T instance) {
FieldInfo[] fi = _fi;
PropertyInfo[] pi = _pi;
if (instance.GetType() != typeof(T)) {
// If the instance is derived from T, extend the table schema
// and get the properties and fields.
ExtendTable(table, instance.GetType());
fi = instance.GetType().GetFields();
pi = instance.GetType().GetProperties();
}
// Add the property and field values of the instance to an array.
Object[] values = new object[table.Columns.Count];
foreach (FieldInfo f in fi) {
values[_ordinalMap[f.Name]] = f.GetValue(instance);
}
foreach (PropertyInfo p in pi) {
values[_ordinalMap[p.Name]] = p.GetValue(instance, null);
}
// Return the property and field values of the instance.
return values;
}
public DataTable ExtendTable(DataTable table, Type type) {
// Extend the table schema if the input table was null or if the value
// in the sequence is derived from type T.
foreach (FieldInfo f in type.GetFields()) {
if (!_ordinalMap.ContainsKey(f.Name)) {
// Add the field as a column in the table if it doesn't exist
// already.
DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name]
: table.Columns.Add(f.Name, f.FieldType);
// Add the field to the ordinal map.
_ordinalMap.Add(f.Name, dc.Ordinal);
}
}
foreach (PropertyInfo p in type.GetProperties()) {
if (!_ordinalMap.ContainsKey(p.Name)) {
// Add the property as a column in the table if it doesn't exist
// already.
DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
: table.Columns.Add(p.Name, p.PropertyType);
// Add the property to the ordinal map.
_ordinalMap.Add(p.Name, dc.Ordinal);
}
}
// Return the table.
return table;
}
}
Now you can add these extensions:
public static class CustomLINQtoDataSetMethods {
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) {
return new ObjectShredder<T>().Shred(source, null, null);
}
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source,
DataTable table, LoadOption? options) {
return new ObjectShredder<T>().Shred(source, table, options);
}
}
Now CopyToDataTable works with any kind of IEnumerable<T>(even anonymous types) not only with DataRows.

c# Write null field to a datatable

`I'm having a problem writing a null result to a datatable.
My linq query is returning values with which I'm populating a new instance of a class.
My datatable is being created generically and being populated with a generically created datarow.
What happens is my datatable is being created succesfully, the query runs, but when I hit the VAR statement it fails because one of the decimal fields is null. I cannot change this in the class because then I cannot create the datatable.
I need to change this one line I think to make it accept a null value:
moneyvalue = result.moneyvalue,
This is my table definition:
[Table(Name = "t_sdi_traded_product")]
public class t_sdi_traded_product
{
[Column]
public string deal_position_id;
[Column]
public decimal moneyvalue;
[Column]
public string cost_centre;
}
This is my class
public class traded_product
{
public string Deal { get; set; }
public decimal moneyvalue { get; set; }
public string InvolvedPartyId { get; set; }
}
This is my query
var query =
from result in t_sdi_traded_product_hsbc.AsQueryable()
where result.sdi_control_id == current_control_id
select new traded_product()
{
Deal = result.deal_position_id,
moneyvalue = result.moneyvalue,
InvolvedPartyId = result.involved_party_id
}
Here is how I create my datatable and datarow
public static DataTable CreateDataTable(Type animaltype)
{
DataTable return_Datatable = new DataTable();
foreach (PropertyInfo info in animaltype.GetProperties())
{
return_Datatable.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
return return_Datatable;
}
public static DataRow makeRow(object input, DataTable table)
{
Type inputtype = input.GetType();
DataRow row = table.NewRow();
foreach (PropertyInfo info in inputtype.GetProperties())
{
row[info.Name] = info.GetValue(input, null);
}
return row;
}
Now as soon as it hits this part of the code after the "var query" I get the problem:
foreach (var results in query)
{
foreach (PropertyInfo result in results.GetType().GetProperties())
{
string name = result.Name;
foreach (PropertyInfo info in used.GetType().GetProperties())
{
if (result.Name == info.Name)
{
try
{
info.SetValue(used, result.GetValue(results, null), null);
}
catch (NoNullAllowedException e)
{
}
finally
{
info.SetValue(used, DBNull.Value, null);
}
//Console.WriteLine("Result {0} matches class {1} and the value is {2}", result.Name, info.Name, result.GetValue(results,null));
}
}
}
tp_table.Rows.Add(used, tp_table);
}
It fails as soon as it hits the foreach, because the value returned from the database for moneyvalue is null.
I cannot change the class piece to decimal? otherwise the CreateDatable method fails because it says DataTable cannot have a nullable value.
I think your issue is in
select new traded_product()
{
Deal = result.deal_position_id,
moneyvalue = result.moneyvalue, <-- here you need some handling for DBNULL.Value
InvolvedPartyId = result.involved_party_id
}
select new traded_product()
{
Deal = result.deal_position_id,
moneyvalue = result.moneyvalue == DBNull.Value ? 0m : result.moneyvalue,
InvolvedPartyId = result.involved_party_id
}
* Update *
Why not construct your datatable using traded_product and as #user65439 mentioned change your DB class (t_sdi_traded_product) to have a nullable column
[Column]
public decimal? moneyvalue;
Then you just have to handle nulls being returned and converting them to 0 for your not-nullable decimal in your traded_product class
If it is allowed to write NULL values to the database you should make your variable types nullable, for example
[Column]
public decimal? moneyvalue;
instead of
[Column]
public decimal moneyvalue;

How to convert a List into DataTable

I'm getting values from another data table as input to list. Now i need to save those list values into another DataTable.
List:
List<DataRow> list = slectedFieldsTable.AsEnumerable().ToList();
foreach (DataRow dr in slectedFieldsTable.Rows)
{
list.Add(dr);
}
New Data table :
DataRow newRow = tempTable.NewRow();
newRow["Field Name"] = fieldLabel;
newRow["Field Type"] = fieldType;
for(int gg =0 ; gg<list.Count; gg++)
{
tempTable.Rows.Add(????);
}
I'm stuck here in adding up rows in to new data table.
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;
}
Variable declare:
DataTable tempTable = new DataTable();
DataTable slectedFieldsTable = new DataTable();
DataRow newRow;
List<object> list = new List<object>();
Add Column in DataTable:
slectedFieldsTable = new DataTable();
slectedFieldsTable.Columns.Add("Field Name");
slectedFieldsTable.Columns.Add("Field Type");
Add Value in DataTable:
slectedFieldsTable.Rows.Add("1", "AAA");
slectedFieldsTable.Rows.Add("2", "BBB");
slectedFieldsTable.Rows.Add("3", "CCC");
Convert DataTable to List:
foreach (DataRow dr in slectedFieldsTable.Rows)
{
list.Add(dr);
}
Add Column in another DataTable:
tempTable.Columns.Add("Field Name", typeof(string));
tempTable.Columns.Add("Field Type", typeof(string));
Convert List to dataTable:
foreach(DataRow dr in list)
{
newRow = tempTable.NewRow();
newRow["Field Name"] = dr.ItemArray[0].ToString();
newRow["Field Type"] = dr.ItemArray[1].ToString();
tempTable.Rows.Add(newRow);
tempTable.AcceptChanges();
}
use CopyToDataTable() method. CopyToDataTable
IEnumerable<DataRow> query = TempselectedFieldsTable.AsEnumerable().ToList();
// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();
The answer providing the ToDataTable is a very nice start but it is missing some key elements. Namely, it ignores that List item properties may:
...be marked ReadOnly
...use the DisplayName attribute
...have a DefaultValue which the DataColumn should know about
...be Nullable
...be marked BrowsableAttribute(false)
The following is an extension method to return a DataTable and either accounts for the above or provides the means for your code to apply them. It also uses an Interface to get the values from the class object rather than Reflection.
public static DataTable ToDataTable<T>(this IList<T> lst, bool includeAll = true)
{
DataTable dt = new DataTable();
DataColumn dc;
PropertyDescriptor pd;
bool Browsable;
PropertyDescriptorCollection propCol = TypeDescriptor.GetProperties(typeof(T));
for (int n = 0; n < propCol.Count; n++)
{
pd = propCol[n];
Type propT = pd.PropertyType;
dc = new DataColumn(pd.Name);
// if Nullable, get underlying type
// the first test may not be needed
if (propT.IsGenericType && Nullable.GetUnderlyingType(propT) != null )
{
propT = Nullable.GetUnderlyingType(propT);
dc.DataType = propT;
dc.AllowDBNull = true;
}
else
{
dc.DataType = propT;
dc.AllowDBNull = false;
}
// is it readonly?
if (pd.Attributes[typeof(ReadOnlyAttribute)] != null)
{
dc.ReadOnly = ((ReadOnlyAttribute)pd.
Attributes[typeof(ReadOnlyAttribute)]).IsReadOnly;
}
// DefaultValue ...
if (pd.Attributes[typeof(DefaultValueAttribute)] != null)
{
dc.DefaultValue = ((DefaultValueAttribute)pd.
Attributes[typeof(DefaultValueAttribute)]).Value;
}
// caption / display name
dc.ExtendedProperties.Add("DisplayName", dc.Caption);
if (pd.Attributes[typeof(DisplayNameAttribute)] != null)
{
// these are usually present but blank
string theName = ((DisplayNameAttribute)pd.
Attributes[typeof(DisplayNameAttribute)]).DisplayName;
dc.Caption = string.IsNullOrEmpty(theName) ? dc.Caption : theName;
// DGV doesnt use Caption...save for later
dc.ExtendedProperties["DisplayName"] = dc.Caption;
}
Browsable = true;
dc.ExtendedProperties.Add("Browsable", Browsable);
var foo = pd.Attributes[typeof(BrowsableAttribute)];
if (pd.Attributes[typeof(BrowsableAttribute)] != null)
{
Browsable = ((BrowsableAttribute)pd.Attributes[typeof(BrowsableAttribute)]).Browsable;
// no such thing as a NonBrowsable DataColumn
dc.ExtendedProperties["Browsable"] = Browsable;
}
// ToDo: add support for custom attributes
if (includeAll || Browsable)
{
dt.Columns.Add(dc);
}
}
// the lst could be empty such as creating a typed table
if (lst.Count == 0) return dt;
if (lst[0] is IDataValuesProvider)
{
IDataValuesProvider dvp;
// copy the data - let the class do the work
foreach (T item in lst)
{
dvp = (IDataValuesProvider)item;
dt.Rows.Add(dvp.GetDataValues(includeAll).ToArray());
}
}
else
{
List<object> values;
foreach (T item in lst)
{
values = new List<object>();
// only Browsable columns added
for (int n = 0; n < dt.Columns.Count; n++)
{
values.Add(propCol[dt.Columns[n].ColumnName].GetValue(item));
}
dt.Rows.Add(values.ToArray());
}
}
return dt;
}
The method allows you to specify whether columns for non Browsable properties should be added to the DataTable. Rather than hiding the columns later, you can omit them entirely if you want.
An interface proves the means to get the data values from collection members in order (as an alternative to a reflection loop):
public interface IDataValuesProvider
{
IEnumerable<object> GetDataValues(bool includeAll);
}
... on the class:
public class StockItem : IDataValuesProvider
{
public int Id { get; set; }
public string ItemName {get; set;}
[Browsable(false), DisplayName("Ignore")]
public string propA {get; set;}
[ReadOnly(true)]
public string Zone { get; set; }
public string Size {get; set;}
[DisplayName("Nullable")]
public int? Foo { get; set; }
public int OnHand {get; set;}
public string ProdCode {get; set;}
[Browsable(false)]
public string propB { get; set; }
public DateTime ItemDate {get; set;}
// IDataValuesProvider implementation
public IEnumerable<object> GetDataValues(bool IncludeAll)
{
List<object> values = new List<object>();
values.AddRange(new object[] {Id, ItemName });
if (IncludeAll) values.Add(propA);
values.AddRange(new object[] { Zone, Size, Foo, OnHand, ProdCode });
if (IncludeAll) values.Add(propB);
values.Add(ItemDate);
return values;
}
}
Add the data values in the same order as they are listed in your class; be sure to update it when you add properties. The reflection version is still there so you can do it either way.
Finally, there are a few common Attributes which do not have a related DataColumn property. The method stores these for you as ExtendedProperties allowing you to easily apply them to the DGV:
var dtX = someData.ToDataTable();
dgvB.SuspendLayout();
dgvB.DataSource = dtX;
// process extended props
foreach (DataColumn dc in dtX.Columns)
{
// no need to test, the code adds them everytime
//if (dc.ExtendedProperties.ContainsKey("DisplayName"))
//{
dgvB.Columns[dc.ColumnName].HeaderText = dc.ExtendedProperties["DisplayName"].ToString();
//}
//if (dc.ExtendedProperties.ContainsKey("Browsable"))
//{
dgvB.Columns[dc.ColumnName].Visible = (bool)dc.ExtendedProperties["Browsable"];
//}
}
dgvB.ResumeLayout();
Results using a list of the class shown above:
Both OnHand and Foo show the DisplayName and both PropA and PropB are hidden. Most importantly, columns created for ReadOnly and Nullable properties act accordingly.
Try this:
foreach (DataRow dr in list)
{
tempTable.Rows.Add(dr);
}

Categories