BindingList and column flexibility - c#

Everyone raves about BindingList in place of DataTable.
How do you guys overcome the problem of column flexibility? For BindingList I need to define and implement T object. If any new columns needed to be added, I need to add new properties to T object....while in DataTable this is much easier.
BindingList<T> samples = new BindingList<T>();
Is that something you live with or is there a relatively easy way to overcome thing?
using c# 2.0, compact framework.

There are still (occasional) advantages to using DataTable - and having column flexibility is one of them. That being said, there are cons as well.
A small comparison of advantages and disadvantages to each are listed in this blog post.
My personal rule of thumb is to use BindingList<T> to bind to a collection of business objects. In this case, the column issue goes away (you know the columns that are useful in advance already), and it feels much more natural.
DataTable is still useful if you're binding to an unknown thing, and trying to do the parsing at runtime.

Related

Data Binding and UI

I need your opinion about UI and Databinding in WPF.
I had argue with my supervisor about UI & databinding. There is A class, it has ObservableCollection of B class, and B class also has ObservableCollection of C class. These classes are used while communicating with NI CAN and LIN device(it is not case).
On the other hand, in user interface there are bunches of TreeView and DataGrids. There are several instances of A class which is used to store data from devices and files, and they are binded to Treeview and Datagrids. But, he disagrees with this, and says to make separate other list of variables for TreeView and Datagrid to data binding. As he says UI data and other must be separate and I agree with it. But, in this case there are several lists of lists of lists and for example: copy from one list to other list when there is change on data takes much time and calculation ( -> UI element ). And other problems also.
I need your your opinion or other advice to come out better solution.
Thank you!!
It sounds like you are a student and doing this work as part of your studies. Either way, there is something to learn here.
Why not implement both approaches and see how they differ, and which you prefer? As it stands your question does not provide enough information for anyone else to tell you, and there is no way to short cut this kind of learning in my experience.

Dynamically create a class and then create a list<dynamicClass> based on it in C#

I want to create a class and its properties on run time, the properties will be like Year2001, Year2002, Year2003, Year2004, Year2005... I get these property names on run-time, I get them in a list. Later I need to use this class to create a list which I need to show in the kendo grid.I surfed a lot and thought of using ExpandoObject, but was unsuccessful.
If all properties will be of the form YearX and contain some information about or related to that year, then I would strongly recommend you (if at all possible) to go with something along the lines of an IList<YearInfo> where YearInfo is some object containing the info you need for every year, including an integer property indicating what year the object corresponds to. If you require these objects to be unique you could use an IDictionary<int, YearObject> or ISet<YearObject> instead.
Reflection can be powerful, but it it comes at the price of complexity and loss of type safety/compile-time checks. Avoid when possible.
Sounds to me like you are really wanting to a grid with grouping support. Your idea of having the system create a CLASS at runtime is not going to fly. Even if it were possible, which I doubt it is, it is absolutely the wrong approach.
Like I say - have a read about Grouping / Hierarchy on Grid Controls (Kendo grid example here), and maybe have a look at OLAP cubes as well...
Although you have had some answers I would also like to suggest an alternative way of doing this which is using DataTables. This is the approach I take when I have any "Dynamic" data sets that I want to present to the grid.
This is also the approach that Telerik themselves take with one of their code samples.
here are a couple of links to show them doing this to DataTables and Dynamic Objects
Grid Binding to Data Table
Grid Binding to Dynamic Objects
Personally I find the binding to Tables easier to deal with as I am used to dealing with Data Tables.

What is the best way to make a listobject binded with datatable

In advance, I'm sorry for my English.
My question is about ListObject and DataTable with C# for Excel. I used to work a lot with Excel VBA in the past and I understand the concept of ListObject. I used to work with C# especially in a ASP.NET context and I can easily figure out about the utility of a DataTable object.
Like I thought, DataTable and Listobject can work together by doing what they are made for :
ListObject will be used as a way to display information (but also has some features that allow some fancy operations on data).
DataTable his a data container offering complete operations acting mostly like an object where observers (like DataGridView) can observe to update their data.
With these lines :
fooListObject.AutoSetDataBoundColumnHeaders = true;
fooListObject.SetDataBinding(fooDataTable);
It's possible to work on ListObject with DataTable header names by (adding row, updating cells, etc) and take advantage of the event callbacks of the DataTable (executing SQL on changes), but the inverse seems to be possible too.
In my application, I have a DataTable linked to a .dbml (LINQ to SQL) object. I also registered a DataRowChangeEventArgs event for that DataTable.
I want to know the best methods to call on ListObject or DataTable to update the model with the latest SQL data. I also want to know what should do each one. Right now, if I delete a row in database that I'm not supposed to see in my Excel view, I see it anyways.
Can you tell me more about interaction between ListObject and DataTable? Some useful methods?
Maybe you are looking for something like the ListObject.SetDataBinding Method.
Check this example: https://msdn.microsoft.com/en-us/library/c5f64c2x.aspx

DataTable vs. Collection in .Net

I am writing a program that needs to read a set of records that describe the register map of a device I need to communicate with. Each record will have a handfull of fields that describe the properties of each register.
I don't really need to edit or modify the data in my VB or C# program, though I would like to be able to display the data on a grid. I would like to store the data in a CSV file, or perhaps an XML file. I need to enable users to edit the data off-line, preferably in excel.
I am considering using a DataTable or a Collection of "Register" objects (which I would define).
I prototyped a DataTable, and found I can read/write XML easily using the built in methods and I can easily bind to a DataGridView. I was not able to find a way to retreive info on a single register without using a query that returns a collection of rows, even though I defined a unique primaty key column. The syntax to get a value from a column is also complex, though I could be missing something on both counts.
I'm tempted to use a collection of "Register" objects that I can access via a unique key. It would be a little more coding up front, but seems like a cleaner solution overall. I should still be able to use LINQ to dataset to query subsets of registers when I need them, but would also be able to grab a single field using a the key value, something like this: Registers(keyValue).fieldName).
Which would be a cleaner approach to the problem?
Is there a way to read/write XML into a Collection without needing custom code?
Could this be accomplished using String for a key?
UPDATE: Sounds like the consensus is towards the Collection of register Objects. Makes sense to me. I was leaning that way, and since nobody pointed out any DataTable features that would simplify acessing a single row, it looks like the Collection is clearly the way to go. Thanks to those who weighed in.
I would be inclined not to use data sets. It would be better to work with objects and collections. Your code will be more maintainable/readable, composable, testable & reusable.
Given that you can do queries on the data set to return particular row, you might find that a LINQ query to turn the rows into objects may be all the custom code that you need.
Using a Dictionary<string, Register> for look ups is a good idea if you have a large number of items (say greater than 1000). Otherwise a simple LINQ query should be fine.
It depends on how you define 'clean'.
A generic collection is potentially MUCH more lightweight than a DataTable. But on the other hand that doesn't seem to be too much of an issue for you. And unless you go into heavy reflection you'll have to write some code to read/write xml.
If you use a key I'd also recommend (in the case of the collection) to use a Dictionary. That way you have a Collection of the raw data and still can identify each entry through the key in the Dictionary.
I usually use datatables if its something quick and unlikely to be used in any other way. If it's something I can see evolving into an object that has its own use within the app (like your Register Object you mentioned).
It might be a little extra code up front - but it saves converting from a datatable to the collection in the future if you come up with something you would like to do based on an individual row, or if you want/need to add some sort of extra functionality to that element down the road.
I would go with the collection of objects so you can swap out the data access later if you need to.
You can serialize classes with an xml serializer and defining a Serialize attribute or something like that (it has been a while since I done that, sorry for the vagueness). A DataSet or DataTable works great with XML.
Both DS and DT have ReadXml and WriteXml methods. XML must be predefined format, but it works seamlessly.
Otherwise, I personally like collections or dictionaries; DS/DT are OK, but I like custom objects, and LINQ adds in some power.
HTH.

Using ASP.NET Repeaters with Object Properties

I'm trying to use ASP.NET's Repeater objects to loop over properties of an object.
For example... I have an ObjectDataSource to grab object "Program" by ID...
Program has properties such as Program.Stakeholders and Program.Outcomes which are Lists of "Stakeholder" and "Outcome" objects.
Now... what I'd really like to do is use the Repeaters to target these Properties and loop over the lists they contain. However, as far as I know I'd have to set up a separate data source for each one, tied to an individual method to retrieve each list.
Can anyone provide a better way to use these Repeater objects, or point me at some resources which would help? If this doesn't make sense I can try to clarify it more.
Using the built-in ObjectDataSource mapping up a separate datasource for each item is probably the only straightforward way (and the only way that's easy enough to be worth the effort...).
Is it a requirement that you use the ObjectDataSource, or can you choose a different way to get the data from the storage? I would recommend either using Entity Framework (which imho rocks) or creating your own custom types to which you get the data with a custom designed DAL (which is a lot more work than using EF, but if you're, like some, concerned that EF is still in infancy this might be your option).
In either case, you'll end up with a C# class called Program, which has properties of type IEnumerable<Stakeholder> and IEnumerable<Outcome> called Stakeholders and Outcomes respectively. You can then use these as datasources for the item repeaters and set them in the ItemDataBound event of the ProgramRepeater, maybe something like this:
protected void ProgramRepeater_ItemDataBound(object sender, ItemDataBoundEvent e) {
Program dataItem = (Program)e.DataItem;
Repeater stakeholderRptr = (Repeater)e.Item.FindControl("ProgramRepeater");
Repeater outecomeRptr = (Repeater)e.Item.FindControl("OutcomeRepeater");
stakeholderRptr.DataSource = dataItem.Stakeholders;
stakeholderRptr.DataBind();
outecomeRptr.DataSource = dataItem.Outcomes;
outecomeRptr.DataBind();
}
This is assuming that you're using ASP.Net WebForms, of course. In ASP.Net MVC it is even easier - you just send the Program object to the View as the Model object, and loop through its Stakeholders and Outcomes in a couple of nested for loops directly on the View.
Note: All code is provided as is, and I do not guarantee that it will run as expected or even compile. It is just to give you an idea of what to make your code do - not necessarily the exact code you need to solve your problem.

Categories

Resources