Bind complex list to datasource a gridview - c#

whether possible Bind complex list to DataSource a GridView with all members?
such as:
public class Car
{
private string _make;
private string _model;
private int _year;
private int _speedCollection;
public Car(string make, string model, int year)
{
_make = make;
_model = model;
_year = year;
}
public string Make
{
get { return _make; }
set { _make = value; }
}
public string Model
{
get { return _model; }
set { _model = value; }
}
public int Year
{
get { return _year; }
set { _year = value; }
}
public List<MyClass> SpeedColections
{
get { return _speedCollection; }
set { _speedCollection = value; }
}
}
public class MyClass
{
private int _speed;
public int Speed
{
get { return _speed; }
set { _speed = value; }
}
}

Yes, it is possible. And it will work, except for the SpeedCollections member, unless you specify another public member that would return, say, a string representation of Speed (comma separated values or something of this sort)
Update
This is an example of a member that would return a string representation of SpeedCollections:
Warning! Potential pseudo-code ahead, I cannot currently compile or test, so make your adjustments when needed
public string SpeedRepresentation
{
get
{
return string.Join(",",
_speedCollection.Select(s => s.Speed().ToString())
.ToArray());
}
}

Related

AsParallel writing to same object

I have a Dictionary with Key-Value pair where Value is a complex Object.
Becaouse of a speed I have used AsParalle().
Problem that occures is that in some of my return objects are placed multiple results of other Key-Value pair.
For examle:
ResutDict has multiple Key-Value pairs.
public IDictionary<string, IOutboundResult> ResultDict
{
get { return m_ResultDict; }
set { m_ResultDict = value; }
}
ResultDict.AsParallel()
.WithDegreeOfParallelism(amountOfThreads)
.ForAll(kvp => { DO YOUR THING AND RETURN OBJECT S_Out}
All of them should return one Object. That later will be serialized into XML.
public class S_Out : ISerializable
{
private List<details> detailField;
[System.Xml.Serialization.XmlElement("details")]
public List<details> details
{
get { return detailField; }
set { detailField = value; }
}
}
public class details
{
private string idField;
private string countryField;
private string town_cityField;
private string postcodeField;
private List<extract> extractField;
public string id
{
get { return idField; }
set { idField = value; }
}
public string country
{
get { return countryField; }
set { countryField = value; }
}
public string town_city
{
get { return town_cityField; }
set { town_cityField = value; }
}
public string postcode
{
get { return postcodeField; }
set { postcodeField = value; }
}
[XmlElement("extract")]
public List<extract> extract
{
get { return extractField; }
set { extractField = value; }
}
}
This list of details is there only for some other cases, but in this particular case should contain only one detail.
During Parallel different Threads write in there so in some files I have no detail object, and in some of them whole list of details.
It should be one detail for one xml file.
Have anyone Idea why is this happening?

Custom Collection/List To Store Custom Objects

I have a customer object class:
public class customerObject
{
private string _address1;
private string _address2;
private string _address3;
private string _category;
private string _country;
private string _county;
private string _custcode;
private string _fullname;
private string _int_rep_hou;
private string _int_rep_key;
private double _lat;
private double _lng;
private string _postcode;
private string _rep_code;
private string _telephone;
public customerObject()
{
}
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}
public string Address2
{
get
{
return _address2;
}
set { _address2 = value; }
}
public string Address3 { get { return _address3; } set { _address3 = value; } }
public string Category
{
get { return _category; }
set { _category = value; }
}
public string Country { get { return _country; } set { _country = value; } }
public string County { get { return _county; } set { _county = value; } }
public string Custcode
{
get { return _custcode; }
set { _custcode = value; }
}
public string Fullname
{
get { return _fullname; }
set { _fullname = value; }
}
public string Int_rep_hou
{
get { return _int_rep_hou; }
set { _int_rep_hou = value; }
}
public string Int_rep_key
{
get { return _int_rep_key; }
set { _int_rep_key = value; }
}
public double Lat { get { return _lat; } set { _lat = value; } }
public double Lng { get { return _lng; } set { _lng = value; } }
public string Postcode { get { return _postcode; } set { _postcode = value; } }
public string Rep_code
{
get { return _rep_code; }
set { Rep_code = value; }
}
public string Telephone { get { return _telephone; } set { _telephone = value; }
}
}
I have a CustomCollections class
public class CustomerCollection
{
public List<customerObject> Customers { get; set; }
}
My method that loops through dt rows and converts to a customer object
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//Create Collection Object
Valueobjects.CustomerCollection Collection = new Valueobjects.CustomerCollection();
foreach (DataRow row in temptablename.Rows)
{
//Create Customer Object
Valueobjects.customerObject Customer = new Valueobjects.customerObject();
//set values of customer object
Customer.Rep_code = "";
Customer.Int_rep_key = "";
Customer.Int_rep_hou = "";
Customer.Fullname = row["Fullname"].ToString().Trim();
Customer.Custcode = row["Custcode"].ToString().Trim();
Customer.Category = row["Category"].ToString().Trim();
Customer.Address1 = row["Address1"].ToString().Trim();
Customer.Address2 = row["Address2"].ToString().Trim();
Customer.Address3 = row["Address3"].ToString().Trim();
Customer.Postcode = row["Postcode"].ToString().Trim();
Customer.Country = row["Country"].ToString().Trim();
Customer.Telephone = row["Telephone"].ToString().Trim();
Customer.Lat = Convert.ToDouble(row["Lat"]);
Customer.Lng = Convert.ToDouble(row["Lng"]);
Customer.County = row["County"].ToString().Trim();
//add to the collection (list)
Collection.Customers.Add(Customer);
}
temptablename = null;
return Collection;
}
However when I create a new Customer object and a new CustomerCollection object I am getting an error when adding the customer to the collection list.
Error:
Error 32 Cannot implicitly convert type
'Classes.Valueobjects.CustomerCollection' to
'System.Collections.Generic.List'
Your method is returning a List<CustomerCollection>:
public List<Valueobjects.CustomerCollection> dolist(DataTable temptablename)
{
//...
}
But the code is trying to return a CustomerCollection:
return Collection;
Just as the error says, these two types are different.
If a CustomerCollection is already a collection of customers, then semantically what is a List<Valueobjects.CustomerCollection>? A collection of collections? It seems like you're over-pluralizing your objects :)
There are two approaches here. Either return a CustomerCollection from the method:
public CustomerCollection dolist(DataTable temptablename)
{
//...
}
Or use a List<Customer> if you want to use generic lists as your collection containers:
public List<Customer> dolist(DataTable temptablename)
{
//...
var Collection = new List<Customer>();
//...
Collection.Add(Customer);
//...
return Collection;
}
Side note: You may want to stick to C# conventions for variable naming. As you can see from the code highlighting here on Stack Overflow, your variable names can easily be mistaken for classes/types, which can cause confusion when supporting the code.
Return a CustomerCollection instead of a List<Valueobjects.CustomerCollection>:
public Valueobjects.CustomerCollection Dolist(DataTable temptablename)
{
// ...
Your object has a list, it is not a list.
MSDN: Inheritance

How do you XMLSerialize a more complex class in c#?

hello I want to serialize a class in C# with a XMLserializer. The Compiler says that Room (myClass) is not primitive (i am using Unity3d) and therefor can not be serialized. The problem is that the most Tutorial are only for simple classes with a few simple fields. Can you help me changs Room to be serializable?
[Serializable]
public class Room
{
public Room()
{
m_id = "test";
m_section = Areas.PublicSec;
m_connections = null;
m_size = 10;
}
public Room(string _id, Areas _section, List<string> _connections, int _size)
{
// f.e. Bathroom
m_id = _id;
// private, public or hallway filled with the enum Areas
m_section = _section;
// list of rooms that have or will have a Connection
if (_connections != null)
{
m_connections = _connections;
}
else
{
m_connections = new List<string>();
}
//? Desired minium size of the room
m_size = _size;
}
private string m_id;
public string id
{
get { return m_id; }
set { m_id = value;}
}
private Areas m_section;
public Areas Section
{
get { return m_section; }
set { m_section = value; }
}
private List<string> m_connections;
[XmlArray("m_connections")]
public List<string> Connections
{
get { return m_connections; }
set { m_connections = value; }
}
private int m_size;
public int Size
{
get { return m_size; }
set { m_size = value; }
}
}
My XMLSerializer works with simple classes but not with this one.

How to use the Same Class on Client as on the Service?

How can I pass an entire defined class through a WCF service? I have the class defined on both the service and client side. I keep getting an error:
Best overloaded method match has some invalid arguments.
The whole class was copied from the client-side to the service-side.
Client side calling:
TransferProxy.PutTransferOnService(Transfer);
Defined on service:
[OperationContract]
bool PutTransferOnService(TypeTransfer Transfer);
I don't want to access individual items on the class from the client, I just want to move the WHOLE populated object through and do processing on the server side.
[DataContract]
public class TypeTransfer
{
private string userID;
private string transferNum;
private DateTime effectiveDate;
private int unitCount;
private int skuCount;
private string reason;
private string localStatus;
private string destStatus;
private string carrier;
private string sourceStore;
private string destinationStore;
private string inSeal;
private string outSeal;
[DataMember]
private List<TypeSOQ> correspondingSOQ = new List<TypeSOQ>();
[DataMember]
private List<TypeProductList> ProductList = new List<TypeProductList>();
public TypeTransfer() { }
// Function adds single item to transfer object
public void AddItem(int ProductID, string SKU, string PrimarySKU, string SCC, string ProductDescription, int TransferQty)
{
ProductList.Add(new TypeProductList
{
productID = ProductID,
sku = SKU,
primaryUPC = PrimarySKU,
scc = SCC,
description = ProductDescription,
transferQty = TransferQty
});
}
// Add SOQ to transfer object (can support multiple SOQ's)
public void AddSOQ(TypeSOQ soq)
{
correspondingSOQ.Add(soq);
}
// Function returns number of skus in Product List
public int GetSKUTotal()
{
return ProductList.Count();
}
// Function returns total number of items in transfer
public int GetItemTotal()
{
int itemtotal = 0;
for (int i = 0; i < ProductList.Count(); i++)
{
itemtotal += ProductList[i].transferQty;
}
return itemtotal;
}
// Return entire SOQ list
public List<TypeSOQ> GetSOQs()
{
return correspondingSOQ;
}
// Returns full product list in transfer object
public List<TypeProductList> GetProductList()
{
return ProductList;
}
[DataMember]
public string UserID
{
get { return userID; }
set { userID = value; }
}
[DataMember]
public string TransferNum
{
get { return transferNum; }
set { transferNum = value; }
}
[DataMember]
public DateTime EffectiveDate
{
get { return effectiveDate; }
set { effectiveDate = value; }
}
[DataMember]
public int UnitCount
{
get { return unitCount; }
set { unitCount = value; }
}
[DataMember]
public string Reason
{
get { return reason; }
set { reason = value; }
}
[DataMember]
public string LocalStatus
{
get { return localStatus; }
set { localStatus = value; }
}
[DataMember]
public string DestStatus
{
get { return destStatus; }
set { destStatus = value; }
}
[DataMember]
public string Carrier
{
get { return carrier; }
set { carrier = value; }
}
[DataMember]
public string SourceStore
{
get { return sourceStore; }
set { sourceStore = value; }
}
[DataMember]
public string DestStore
{
get { return destinationStore; }
set { destinationStore = value; }
}
[DataMember]
public string InSeal
{
get { return inSeal; }
set { inSeal = value; }
}
[DataMember]
public string OutSeal
{
get { return outSeal; }
set { outSeal = value; }
}
[DataMember]
public int SKUCount
{
get { return skuCount; }
set { skuCount = value; }
}
}
You said - The whole class was copied from the client-side to the service-side.
You don't need to copy your class to server side. just define your class in a separate library and give reference of that same library to both client and server.

inner classes and variables in WCF service class

I've got class in WCF. Inside this class - there are 2 classes, 1 interface and several variables. It adds as service reference without errors.
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class XmlService
{
private string filename;
private XmlTextReader xmlreader;
List<IWeather> returner=new List<IWeather>();
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
interface IWeather
{
string GetCondition { get; set; }
DateTime GetDate { get; set; }
}
public class Current_Weather:IWeather
{
private string condition, humidity, wind_condition;
private int temp_f, temp_c;
private DateTime day;
public string GetCondition
{
get { return condition; }
set { condition = value; }
}
public string GetHumidity
{
get { return humidity; }
set { humidity = value; }
}
public string GetWindCondition
{
get { return wind_condition; }
set { wind_condition = value; }
}
public int TEMP_F
{
get { return temp_f; }
set { temp_f = value; }
}
public int TEMP_C
{
get { return temp_c; }
set { temp_c = value; }
}
public DateTime GetDate
{
get { return day; }
set { day = value; }
}
}
public class Forecast_Weather:IWeather
{
public string condition;
public int lowT, highT;
public DateTime day;
public string GetCondition
{
get { return condition; }
set { condition = value; }
}
public int GetLowT
{
get { return lowT; }
set { lowT = value; }
}
public int HighT
{
get { return highT; }
set { highT = value; }
}
public DateTime GetDate
{
get { return day; }
set { day = value; }
}
}
}
Should I add contracts to variables,interface IWeather, for inner classes and its methods and variables?
If you want them serialzied and visible on the client they need to be marked with contract attributes.
Internal classes aren't really a good practice, instead put all your operations in one interface marked up as a service contract, then all your data contracts in their own class libraries so you can reference that assemply frmo your client. This facilitates writing your own proxies and other good habits.
Your internal classes will not be exposed to callers of your service, since your service doesn't use them either as parameters or as return values.

Categories