Only getting Length of string for converting list to datatable
I'm writing a program in C# VS 2012 WinForms and found this block of code
on StackOverflow to transfer a list to a datatable.
public static DataTable ToDataTable(List<String> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(String));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name,
Nullable.GetUnderlyingType(prop.PropertyType) ??
prop.PropertyType);
foreach (string 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;
}
I was wondering if there was a way to return the actual string itself and
not just the length of the string. If anyone can point me in the right
direction that would be much appreciated. Thanks.
No comments:
Post a Comment