I'm working on a project that connects to Oracle. It Brings back data through a dataset. I use Linq to bind it to a collection and throw it back to be read by json. It works great but I can't help but think - there's got to be a better way to do this. Here's an example of what I do. I hope it helps others. Dsp is Dataset.
List<Information> lstSearch = null;
lstSearch = (from l in dsp.Tables[0].AsEnumerable()
select new Information
{
application_id = l["APPLICATION_ID"].ToString(),
hospital_name_1 = l["HOSPITAL_NAME_"].ToString(),
physical_address = l["PHYSICAL_ADDRESS"].ToString(),
// may have to add more here...
}).ToList<Information>();
// serialize and send back as a json string
System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(lstSearch.First());
Theoretically, yes works. The "information" collection matches the html "name" tag of each control on the page which provides a nice robust binding. My concern lies with having
to go through each field name in order to populate the List<> object.
Isn't there a specific where clause in where the collection (get/set) property matches the dataset column name thus populating the collection only if the column name (not the value) matches the data row column?
I'd recommend AutoMapper ... see this question.
Here's a very simple example. Make sure that your .NET type exactly mirrors the structure of your DB object or AutoMapper will not work as advertised:
namespace EnumerableDT
{
class Program
{
public class Information
{
public int APPLICATION_ID { get; set; }
public string HOSPITAL_NAME { get; set; }
public string PHYSICAL_ADDRESS { get; set; }
public string SOME_OTHER_FIELD { get; set; }
}
static DataSet dsp = new DataSet();
static void Main(string[] args)
{
dsp.Tables.Add(BuildDataTableStructure());
dsp.Tables[0].Rows.Add(BuildRow());
AutoMapper.Mapper.Reset();
AutoMapper.Mapper.CreateMap<IDataReader, Information>();
var il = AutoMapper.Mapper.Map<IDataReader, IList<Information>>(dsp.Tables[0].CreateDataReader());
Console.ReadLine();
}
static DataTable BuildDataTableStructure()
{
var dt = new DataTable();
var dc = new DataColumn("APPLICATION_ID", typeof(int));
dt.Columns.Add(dc);
dc = new DataColumn("HOSPITAL_NAME", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("PHYSICAL_ADDRESS", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("SOME_OTHER_FIELD", typeof(string));
dt.Columns.Add(dc);
return dt;
}
static DataRow BuildRow()
{
DataRow dr = dsp.Tables[0].NewRow();
dr["APPLICATION_ID"] = 1;
dr["HOSPITAL_NAME"] = "The Hospital";
dr["PHYSICAL_ADDRESS"] = "123 Main St.";
dr["SOME_OTHER_FIELD"] = "Some Other Data";
return dr;
}
}
}
Related
I have a DataTable with 3x string columns, a constraint and I am trying to fill it with unique rows. The first row adds nicely but unfortunately when I am trying to add a second one with unique values I am receiving a false constraint error message.
I have distilled my custom class to the minimum and added an extra check for the two values before trying to add them, but the result is contradictory.
Here it goes:
using System.Data;
using System.Collections;
namespace DataTableForeignKey
{
public class Symbols : IEnumerable
{
protected DataTable fTable = new DataTable();
protected DataColumn fCategoryColumn = new DataColumn("Category", typeof(string));
protected DataColumn fNameColumn = new DataColumn("Name", typeof(string));
protected DataColumn fValueColumn = new DataColumn("Value", typeof(string));
public Symbols()
{
fTable.Columns.Add(fCategoryColumn);
fTable.Columns.Add(fNameColumn);
fTable.Columns.Add(fValueColumn);
// Temporarily disabled
//fTable.Constraints.Add(new ForeignKeyConstraint(fNameColumn, fCategoryColumn));
fTable.Constraints.Add(new ForeignKeyConstraint(fValueColumn, fCategoryColumn));
}
public virtual void Add(string aCategory, string aName, string aValue)
{
var lRow = fTable.NewRow();
lRow[fCategoryColumn] = aCategory;
lRow[fNameColumn] = aName;
lRow[fValueColumn] = aValue;
fTable.Rows.Add(lRow);
}
IEnumerator IEnumerable.GetEnumerator()
{
return fTable.Rows.GetEnumerator();
}
}
class Program
{
static void Main(string[] args)
{
var lSymbols = new Symbols();
var lWhitespace = " ";
var lEmptyString = string.Empty;
if (lWhitespace != lEmptyString)
{
lSymbols.Add("Whitespace", "Separator", lWhitespace);
lSymbols.Add("Prefix", "Command", lEmptyString);
// The second Add() throws the following exception:
// System.Data.ConstraintException: 'Column 'Value' is
// constrained to be unique. Value '' is already present.'
}
}
}
}
How can an empty string match with a whitespace?
Many thanks for the answers in advance.
What you are looking for is a UniqueConstraint. So you can change that line of code to:
fTable.Constraints.Add(new UniqueConstraint(new DataColumn[] { fValueColumn, fCategoryColumn}));
I am making an application that stores data that user inputs.
http://i827.photobucket.com/albums/zz200/ArnasG/question_in_stackowerflow__zps4f7uy3l7.png
I have a bit of an issue because of my lack of experience, I want to save all the data user inputs in XML file and to load it when program starts next time. I had an idea to use dataset to read all the data from XML file and then work with the table[0] of that dataset(add/delete rows). It turn out that I can not make it to work properly. It loads some blank lines and lines that I created in previous tries, but there is only two lines that are actually saved in XML file. How could I make this work?
Thank you for your time :)
Actual XML file:
http://i827.photobucket.com/albums/zz200/ArnasG/question_in_stackowerflow_V2_zpshmwjnllr.png
DataSet ListOfTrades = new DataSet();
DataTable Lentele = new DataTable();
ListOfTrades.Tables.Add(Lentele);
// adding columns to the table
try
{
DataColumn Pair = new DataColumn("Pair", typeof(string));
Pair.AllowDBNull = false;
DataColumn Entry = new DataColumn("Entry", typeof(string));
Entry.AllowDBNull = false;
DataColumn StopLoss = new DataColumn("StopLoss", typeof(string));
StopLoss.AllowDBNull = false;
DataColumn TakeProfit = new DataColumn("TakeProfit", typeof(string));
TakeProfit.AllowDBNull = false;
DataColumn TakeProfit1 = new DataColumn("TakeProfit1", typeof(string));
TakeProfit1.AllowDBNull = false;
DataColumn TakeProfit2 = new DataColumn("TakeProfit2", typeof(string));
TakeProfit2.AllowDBNull = false;
DataColumn TakeProfit3 = new DataColumn("TakeProfit3", typeof(string));
TakeProfit3.AllowDBNull = false;
DataColumn LongShort = new DataColumn("LongShort", typeof(string));
LongShort.AllowDBNull = false;
DataColumn WinLoss = new DataColumn("WinLoss", typeof(string));
WinLoss.AllowDBNull = false;
data.Tables[0].Columns.AddRange(new DataColumn[] {
Pair, Entry, StopLoss, TakeProfit, TakeProfit1, TakeProfit2,
TakeProfit3, LongShort, WinLoss
});
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
// Adding new line to the table after user clicks save button
private void button1_Click(object sender, EventArgs e)
{
DataRow eilute = ListOfTrades.Tables[0].NewRow();
eilute[0] = comboBox1.Text.ToString();
eilute[1] = textBox1.Text.ToString();
eilute[2] = textBox2.Text.ToString();
eilute[3] = textBox3.Text.ToString();
eilute[4] = textBox4.Text.ToString();
eilute[5] = textBox5.Text.ToString();
eilute[6] = textBox6.Text.ToString();
if (radioButton1.Checked) { eilute[7] = "Long"; }
else { eilute[7] = "short"; }
if (radioButton1.Checked) { eilute[8] = "Win"; }
else { eilute[8] = "Loss"; }
ListOfTrades.Tables[0].Rows.Add(eilute);
ListOfTrades.Tables[0].WriteXml(DefaultPathToJournalXML);
dataGridView1.Update();
dataGridView1.Refresh();
}
Not getting duplicated. here is the xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table1>
<Pair>AUD/USD</Pair>
<Entry>0.00000</Entry>
<StopLoss>0.00000</StopLoss>
<TakeProfit>0.00000</TakeProfit>
<TakeProfit1>0.00000</TakeProfit1>
<TakeProfit2>0.00000</TakeProfit2>
<TakeProfit3>0.00000</TakeProfit3>
<LongShort>short</LongShort>
<WinLoss>loss</WinLoss>
</Table1>
<Table1>
<Pair>AUD/USD</Pair>
<Entry>0.00000</Entry>
<StopLoss>0.00000</StopLoss>
<TakeProfit>0.00000</TakeProfit>
<TakeProfit1>0.00000</TakeProfit1>
<TakeProfit2>0.00000</TakeProfit2>
<TakeProfit3>0.00000</TakeProfit3>
<LongShort>short</LongShort>
<WinLoss>Loss</WinLoss>
</Table1>
</NewDataSet>
Here is code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
DataSet ds = new DataSet();
ds.ReadXml(FILENAME);
}
}
}
Think object oriented, and think Linq.
For example, let's say you have this XML file:
<trades>
<trade>
<pair>some pair</pair>
<stop-loss>stop loss 1</stop-loss>
</trade>
<trade>
<pair>other pair</pair>
<stop-loss>stop loss 2</stop-loss>
</trade>
</trades>
You can create a Trade class to hold the data in the trade tags and then use a Linq query to populate the object given the XML file:
class Trade
{
public string Pair;
public string StopLoss;
// Other variables from trade tag would go here...
// Function that can load trade objects from XML file into a list of Trade objects (List<Trade>)
public static List<Trade> loadTrade(string xmlFilePath)
{
// Load your XML document given the path to the .xml file
var doc = XDocument.Load(xmlFilePath);
// For each trade element in the trades element
var trades = (from trade in doc.Element("trades").Elements("trade")
select new Trade
{
// For each element in the trade element, put value in class variable
Pair = trade.Element("pair").Value,
StopLoss = trade.Element("stop-loss").Value
}).ToList<Trade>();
return trades;
}
}
When you're ready to save to a file you basically do the opposite of the Linq query to create an XML file. It will look very similar.
On there other hand, read this article and consider if there's a better alternative.
I've just learned about Generics and I'm wondering whether I can use it to dynamically build datatables from my classes.
Or I might be missing the point here.
Here is my code, what I'm trying to do is create a datatable from my existing class and populate it. However I'm getting stuck in my thought process.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
namespace Generics
{
public class Dog
{
public string Breed { get; set; }
public string Name { get; set; }
public int legs { get; set; }
public bool tail { get; set; }
}
class Program
{
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;
}
static void Main(string[] args)
{
Dog Killer = new Dog();
Killer.Breed = "Maltese Poodle";
Killer.legs = 3;
Killer.tail = false;
Killer.Name = "Killer";
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(Dog);
//How do I continue from here?
}
}
}
Now At the DataTable point it errors.
Also, being new to reflection and Generics, how will I actually populate the data with the Killer class?
Building up on all the previous answers, here is a version that creates a DataTable from any collection:
public static DataTable CreateDataTable<T>(IEnumerable<T> list)
{
Type type = typeof(T);
var properties = type.GetProperties();
DataTable dataTable = new DataTable();
dataTable.TableName = typeof(T).FullName;
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;
}
Here is a more compact version of David's answer that is also an extension function. I've posted the code in a C# project on Github.
public static class Extensions
{
public static DataTable ToDataTable<T>(this IEnumerable<T> self)
{
var properties = typeof(T).GetProperties();
var dataTable = new DataTable();
foreach (var info in properties)
dataTable.Columns.Add(info.Name, Nullable.GetUnderlyingType(info.PropertyType)
?? info.PropertyType);
foreach (var entity in self)
dataTable.Rows.Add(properties.Select(p => p.GetValue(entity)).ToArray());
return dataTable;
}
}
I have found that this works very well in conjunction with code to write a DataTable to CSV.
my favorite homemade function. it create and populate all at same time. throw any object.
public static DataTable ObjectToData(object o)
{
DataTable dt = new DataTable("OutputData");
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
o.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(o, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(o, null);
}
catch { }
});
return dt;
}
The error can be resolved by changing this:
dogTable = CreateDataTable(Dog);
to this:
dogTable = CreateDataTable(typeof(Dog));
But there are some caveats with what you're trying to do. First, a DataTable can't store complex types, so if Dog has an instance of Cat on it, you won't be able to add that as a column. It's up to you what you want to do in that case, but keep it in mind.
Second, I would recommend that the only time you use a DataTable is when you're building code that knows nothing about the data its consuming. There are valid use cases for this (e.g. a user-driven data mining tool). If you already have the data in the Dog instance, just use it.
Another little tidbit, this:
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(Dog);
can be condensed to this:
DataTable dogTable = CreateDataTable(Dog);
Here is a little bit modified code, which fixed time zone issue for datatime fields:
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
if (props[i].PropertyType == typeof(DateTime))
{
DateTime currDT = (DateTime)props[i].GetValue(item);
values[i] = currDT.ToUniversalTime();
}
else
{
values[i] = props[i].GetValue(item);
}
}
table.Rows.Add(values);
}
return table;
}
Here's a VB.Net version that creates a data table from a generic list passed to the function as an object. There is also a helper function (ObjectToDataTable) that creates a data table from an object.
Imports System.Reflection
Public Shared Function ListToDataTable(ByVal _List As Object) As DataTable
Dim dt As New DataTable
If _List.Count = 0 Then
MsgBox("The list cannot be empty. This is a requirement of the ListToDataTable function.")
Return dt
End If
Dim obj As Object = _List(0)
dt = ObjectToDataTable(obj)
Dim dr As DataRow = dt.NewRow
For Each obj In _List
dr = dt.NewRow
For Each p as PropertyInfo In obj.GetType.GetProperties
dr.Item(p.Name) = p.GetValue(obj, p.GetIndexParameters)
Next
dt.Rows.Add(dr)
Next
Return dt
End Function
Public Shared Function ObjectToDataTable(ByVal o As Object) As DataTable
Dim dt As New DataTable
Dim properties As List(Of PropertyInfo) = o.GetType.GetProperties.ToList()
For Each prop As PropertyInfo In properties
dt.Columns.Add(prop.Name, prop.PropertyType)
Next
dt.TableName = o.GetType.Name
Return dt
End Function
Using the answer provided by #neoistheone I've changed the following sections. Works fine now.
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(typeof(Dog));
dogTable.Rows.Add(Killer.Breed, Killer.Name,Killer.legs,Killer.tail);
foreach (DataRow row in dogTable.Rows)
{
Console.WriteLine(row.Field<string>("Name") + " " + row.Field<string>("Breed"));
Console.ReadLine();
}
you can convert the object to xml then load the xml document to a dataset, then extract the first table out of the data set. However i dont see how this be practical as it infers creating streams, datasets & datatables and using converstions to create the xml document.
I guess for proof of concept i can understand why. Here is an example, but somewhat hesitant to use it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Xml.Serialization;
namespace Generics
{
public class Dog
{
public string Breed { get; set; }
public string Name { get; set; }
public int legs { get; set; }
public bool tail { get; set; }
}
class Program
{
public static DataTable CreateDataTable(Object[] arr)
{
XmlSerializer serializer = new XmlSerializer(arr.GetType());
System.IO.StringWriter sw = new System.IO.StringWriter();
serializer.Serialize(sw, arr);
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dt = new System.Data.DataTable();
System.IO.StringReader reader = new System.IO.StringReader(sw.ToString());
ds.ReadXml(reader);
return ds.Tables[0];
}
static void Main(string[] args)
{
Dog Killer = new Dog();
Killer.Breed = "Maltese Poodle";
Killer.legs = 3;
Killer.tail = false;
Killer.Name = "Killer";
Dog [] array_dog = new Dog[5];
Dog [0] = killer;
Dog [1] = killer;
Dog [2] = killer;
Dog [3] = killer;
Dog [4] = killer;
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(array_dog);
// continue here
}
}
}
look the following example here
If you want to set columns order/ Include only some columns/ exclude some columns try this:
private static DataTable ConvertToDataTable<T>(IList<T> data, string[] fieldsToInclude = null,
string[] fieldsToExclude = null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
{
if ((fieldsToInclude != null && !fieldsToInclude.Contains(prop.Name)) ||
(fieldsToExclude != null && fieldsToExclude.Contains(prop.Name)))
continue;
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in data)
{
var atLeastOnePropertyExists = false;
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
if ((fieldsToInclude != null && !fieldsToInclude.Contains(prop.Name)) ||
(fieldsToExclude != null && fieldsToExclude.Contains(prop.Name)))
continue;
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
atLeastOnePropertyExists = true;
}
if(atLeastOnePropertyExists) table.Rows.Add(row);
}
if (fieldsToInclude != null)
SetColumnsOrder(table, fieldsToInclude);
return table;
}
private static void SetColumnsOrder(DataTable table, params String[] columnNames)
{
int columnIndex = 0;
foreach (var columnName in columnNames)
{
table.Columns[columnName].SetOrdinal(columnIndex);
columnIndex++;
}
}
I am receiving the above error code when attempting to do a SqlBulkInsert of the following "Cities" DataTable:
DataTable cityTable = new DataTable(City.TABLE_NAME);
cityTable.Columns.Add("id", typeof(int));
cityTable.Columns.Add("name", typeof(string));
cityTable.Columns.Add("ascii_name", typeof(string));
cityTable.Columns.Add("alternate_names", typeof(string));
cityTable.Columns.Add("latitude", typeof(double));
cityTable.Columns.Add("longitude", typeof(double));
cityTable.Columns.Add("feature_class", typeof(char));
cityTable.Columns.Add("feature_code", typeof(string));
cityTable.Columns.Add("country_code", typeof(string));
cityTable.Columns.Add("country_code2", typeof(string));
cityTable.Columns.Add("population", typeof(long));
cityTable.Columns.Add("elevation", typeof(int));
cityTable.Columns.Add("modification_date", typeof(DateTime));
cityTable.Columns.Add("admin1code", typeof(string));
cityTable.Columns.Add("admin2code", typeof(string));
cityTable.Columns.Add("admin3code", typeof(string));
cityTable.Columns.Add("admin4code", typeof(string));
cityTable.Columns.Add("gtopo30", typeof(int));
cityTable.Columns.Add("timezone_name", typeof(string));
cityTable.Columns.Add("version", typeof(Binary));
The following is the code that adds each entity to the DataTable:
object id = EvaluateNullity(parsedCity.Id);
object name = EvaluateNullity(parsedCity.Name);
object asciiName = EvaluateNullity(parsedCity.AsciiName);
object alternateNames = EvaluateNullity(parsedCity.AlternateNames);
object latitude = EvaluateNullity(parsedCity.Latitude);
object longitude = EvaluateNullity(parsedCity.Longitude);
object featureClass = EvaluateNullity(parsedCity.FeatureClass);
object featureCode = EvaluateNullity(parsedCity.FeatureCode);
object countryCode = EvaluateNullity(parsedCity.CountryCode);
object countryCode2 = EvaluateNullity(parsedCity.CountryCode2);
object population = EvaluateNullity(parsedCity.Population);
object elevation = EvaluateNullity(parsedCity.Elevation);
object modificationDate = EvaluateNullity(parsedCity.ModificationDate);
object admin1Code = EvaluateNullity(parsedCity.Admin1Code);
object admin2Code = EvaluateNullity(parsedCity.Admin2Code);
object admin3Code = EvaluateNullity(parsedCity.Admin3Code);
object admin4Code = EvaluateNullity(parsedCity.Admin4Code);
object gtopo30 = EvaluateNullity(parsedCity.Gtopo30);
object timeZoneName = EvaluateNullity(parsedCity.TimeZoneName);
object version = EvaluateNullity(parsedCity.Version);
cityRow["id"] = id;
cityRow["name"] = name;
cityRow["ascii_name"] = asciiName;
cityRow["alternate_names"] = alternateNames;
cityRow["latitude"] = latitude;
cityRow["longitude"] = longitude;
cityRow["feature_class"] = featureClass;
cityRow["feature_code"] = featureCode;
cityRow["country_code"] = countryCode;
cityRow["country_code2"] = countryCode2;
cityRow["population"] = population;
cityRow["elevation"] = elevation;
cityRow["modification_date"] = modificationDate;
cityRow["admin1code"] = admin1Code;
cityRow["admin2code"] = admin2Code;
cityRow["admin3code"] = admin3Code;
cityRow["admin4code"] = admin4Code;
cityRow["gtopo30"] = gtopo30;
cityRow["timezone_name"] = timeZoneName;
cityRow["version"] = version;
And the code for EvaluateNullity:
public object EvaluateNullity(object entity)
{
return entity ?? DBNull.Value;
}
My understanding from this error message is that A DateTime value is being placed in one of the above int columns.
However, a quick conditional debug later, and checks in the Immediate Window reveal that none of the int columns ever have DateTime types being placed in them.
http://desmond.imageshack.us/Himg42/scaled.php?server=42&filename=mod1rm.jpg&res=landing
http://desmond.imageshack.us/Himg37/scaled.php?server=37&filename=modyf.jpg&res=landing
I am really stumped.
From David Andres:
Double check that the order of the columns defined in cityTable
matches the order of columns within the database table itself.
One non-obvious cause of this error is that you must have a property representing identity columns, even though you can leave them unset.
For me it was a mismatch between the entity framework model class and the actual table (forgot to scaffold the table model from db):
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Databases
{
using System;
using System.Collections.Generic;
public partial class FooTable
{
public int Id { get; set; }
public string Name { get; set; }
// --> Missing the LastUpdated Column.
}
}
The Table:
I seem to be running around in circles and have been doing so in the last hours.
I want to populate a datagridview from an array of strings. I've read its not possible directly, and that I need to create a custom type that holds the string as a public property. So I made a class:
public class FileName
{
private string _value;
public FileName(string pValue)
{
_value = pValue;
}
public string Value
{
get
{
return _value;
}
set { _value = value; }
}
}
this is the container class, and it simply has a property with the value of the string. All I want now is that string to appear in the datagridview, when I bind its datasource to a List.
Also I have this method, BindGrid() which I want to fill the datagridview with. Here it is:
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;
//create the column programatically
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
colFileName.CellTemplate = cell; colFileName.Name = "Value";
colFileName.HeaderText = "File Name";
colFileName.ValueType = typeof(FileName);
//add the column to the datagridview
gvFilesOnServer.Columns.Add(colFileName);
//fill the string array
string[] filelist = GetFileListOnWebServer();
//try making a List<FileName> from that array
List<FileName> filenamesList = new List<FileName>(filelist.Length);
for (int i = 0; i < filelist.Length; i++)
{
filenamesList.Add(new FileName(filelist[i].ToString()));
}
//try making a bindingsource
BindingSource bs = new BindingSource();
bs.DataSource = typeof(FileName);
foreach (FileName fn in filenamesList)
{
bs.Add(fn);
}
gvFilesOnServer.DataSource = bs;
}
Finally, the problem: the string array fills ok, the list is created ok, but I get an empty column in the datagridview. I also tried datasource= list<> directly, instead of = bindingsource, still nothing.
I would really appreciate an advice, this has been driving me crazy.
Use a BindingList and set the DataPropertyName-Property of the column.
Try the following:
...
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;
//create the column programatically
DataGridViewCell cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn()
{
CellTemplate = cell,
Name = "Value",
HeaderText = "File Name",
DataPropertyName = "Value" // Tell the column which property of FileName it should use
};
gvFilesOnServer.Columns.Add(colFileName);
var filelist = GetFileListOnWebServer().ToList();
var filenamesList = new BindingList<FileName>(filelist); // <-- BindingList
//Bind BindingList directly to the DataGrid, no need of BindingSource
gvFilesOnServer.DataSource = filenamesList
}
may be little late but useful for future. if you don't require to set custom properties of cell and only concern with header text and cell value then this code will help you
public class FileName
{
[DisplayName("File Name")]
public string FileName {get;set;}
[DisplayName("Value")]
public string Value {get;set;}
}
and then you can bind List as datasource as
private void BindGrid()
{
var filelist = GetFileListOnWebServer().ToList();
gvFilesOnServer.DataSource = filelist.ToArray();
}
for further information you can visit this page Bind List of Class objects as Datasource to DataGridView
hope this will help you.
I know this is old, but this hung me up for awhile. The properties of the object in your list must be actual "properties", not just public members.
public class FileName
{
public string ThisFieldWorks {get;set;}
public string ThisFieldDoesNot;
}
Instead of create the new Container class you can use a dataTable.
DataTable dt = new DataTable();
dt.Columns.Add("My first column Name");
dt.Rows.Add(new object[] { "Item 1" });
dt.Rows.Add(new object[] { "Item number 2" });
dt.Rows.Add(new object[] { "Item number three" });
myDataGridView.DataSource = dt;
More about this problem you can find here: http://psworld.pl/Programming/BindingListOfString
Using DataTable is valid as user927524 stated.
You can also do it by adding rows manually, which will not require to add a specific wrapping class:
List<string> filenamesList = ...;
foreach(string filename in filenamesList)
gvFilesOnServer.Rows.Add(new object[]{filename});
In any case, thanks user927524 for clearing this weird behavior!!