|
That's not too bad. It looks similar but actually most of it is not identical and therefore isn't an immediate candidate for refactoring. The only thing that is is creating or activating a tab, which is about 10 lines, which isn't critical.
However, the type switching by name is ugly. You should have ClientModel and ProjectModel both implement an interface (let's call it IModelInfoProvider) that exposes what you need to determine tab identity (an AppViews enum, ModelType, and an ID). You could argue that they should go in the view model, so if you have two way linkage you could put them there and expose a base view model through the interface, or make the UI control have ViewModels not Models as its item list. Then you can do:
IModelInfoProvider item = (IModelInfoProvider)SelectedItem;
TabInfo tabInfo = getTabInfo(item.ViewType, item.ID);
if(tabInfo == null){
TabInfo info = new TabInfo
{
ItemId = item.ID,
ViewType = item.ViewType
};
IView view = ...;
RadTabItem tab = addTab(view.DisplayName, view.IconPath);
tab.Content = view;
tab.Tag = info;
SelectedTab = tab;
} else activateTab(tabInfo.Tab);
Now, what goes in ... is essentially 'make your MV and View'. Depending on how strict on layering you're feeling you can either expose that as an interface method as well, or create a factory to which you can pass a model and it will construct views of the appropriate type. In the latter case you will still have to type switch, inside the factory code – please use is if you have to type switch, not string type names!
|
|
|
|
|
I have a C# 2010 Windows Forms project that accesses (primarily) two tables in a SQL Server 2008 database. Each table is accessed in two modes: active accounts and cancelled accounts, and both have to be visible and editable at the same time (user requirement). The way I have it set up now is to have one DataSet, one TableAdapterManager, two TableAdapters (one for each table) and four BindingSources (two for each table adapter; one for active, one for cancelled). I have FillBy/GetDataBy methods setup for each TableAdapter that fill my BindingSources correctly. This is the code I’m using for filling them:
htcActiveBindingSource.DataSource = this.htcTableAdapter.ActiveGetDataBy(gpn);
htcCancelledBindingSource.DataSource = this.htcTableAdapter.CancelledGetDataBy(gpn);
vetActiveBindingSource.DataSource = this.vetTableAdapter.ActiveGetDataBy(gpn);
vetCancelledBindingSource.DataSource = this.vetTableAdapter.CancelledGetDataBy(gpn);
Everything works exactly as I was hoping, except for saving. The code I was using for saving is this:
this.Validate();
this.htcActiveBindingSource.EndEdit();
this.htcCancelledBindingSource.EndEdit();
this.vetActiveBindingSource.EndEdit();
this.vetCancelledBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.cramdDataSet);
When I step through in debug mode, the current row in the BindingSource knows that it has been modified after the EndEdits run. However, when it executes the UpdateAll, nothing happens in the database. Playing around with it, I figured out that if I fill the TableAdapter directly, like this:
this.htcTableAdapter.ActiveFillBy(this.cramdDataSet.htc, gpn);
then it works, but only for just active or just cancelled, not both at the same time (it sets both binding sources to the same data). Is there some way to get the data from the independent BindingSources back into the TableAdapter? Or am I going to be forced to create a second table adapter for the cancelled versions of each table? Am I going about this in a completely retarded way? If so, please enlighten me! (As much as I’d like to make my code work, I’m not opposed to rewriting big chunks to do it the “right” way.) I’m also going to need to insert new records created from the form, but I haven’t gotten to that point yet since my basic save is not even working.
Thanks in advance!
|
|
|
|
|
Can you show ActiveFillBy and ActiveGetDataBy? I suspect that you are returning a binding source on an in-memory copy of the data and the link back to the database is being lost.
|
|
|
|
|
I'm not sure what you mean by "show ActiveFillBy and ActiveGetDataBy". They are queries attached to the htcTableAdapter in the dataset designer. However, I stumbled upon a way to make it work, but I'm not sure exactly why it works. If I alter the code like this before the UpdateAll, then it works:
this.htcActiveBindingSource.EndEdit();
this.htcTableAdapter.Update((cramdDataSet.htcRow)((DataRowView)this.htcActiveBindingSource.Current).Row);
I get what the update is doing: taking the current row from the BindingSource and applying it back to the TableAdapter. What I don't get is the parameter value (starting w/ the first double parentheses). I found that online somewhere a while ago, but I don't understand what it is doing. Is there a better way to pass the current row into the update? More importantly, is there a way to pass the entire set of rows from the binding source? The vetActiveBindingSource and vetCancelledBindingSource can have multiple records in them that can all be edited. Since one overload of the BindingSource.Update method can take a collection of rows, I'd rather do it like that, rather than looping thru all the records and passing them in one at a time. I looked thru the BindingSource properties and methods but didn't see anything that looked promising.
|
|
|
|
|
Ah, I thought those were methods you'd written. I'm afraid I'm not familiar enough with this piece of Microsoft magic to offer detailed advice. (Anything with auto-generated code I try to avoid using; I like to know what my program is doing! The form designer I will accept as that code is just layout and not function, but data binding templates and the like are too magic for my taste.)
What that statement seems to be doing is casting the binding source to a view and its current row back to the original schema. I would expect you to be able to update through the view, so perhaps there is some option you missed when creating the binding?
|
|
|
|
|
I'm beginning to think the "non-magic" method is the way to go. Even though I got the save working, I keep running into other obstacles trying to make this do what I want. Can you give me some advice on rewriting this manually? Some sample code doing something similar would be great, or a site with a tutorial on setting it up. As long as it doesn't involve a datagridview... I've read several tutorial sites and they seem to only deal with that and it won't work for what I'm trying to do (and I haven't figured out how to convert what they say for the DGV into separate controls).
|
|
|
|
|
You can use the DGV without needing to do so much magic. For example you can bind to a List<T>, and catch the OnCellValueChanged to manually send an update to your data source. If you're using WPF, you can bind to an ObservableList<T>, and use the list notifications to update a data source.
If you're trying to view tabular data, the DGV is a good control to use. It's the binding magic that confuses me.
(edit - html fail)
modified on Wednesday, June 15, 2011 7:28 PM
|
|
|
|
|
Hi experts,
I am developing an application for a customer. The application uses a USB device for which there is a driver. The customer would like the application setup to include the driver installation.
AFAIK the way to go is
1. Copy driver files to some directory I know.
2. Tell windows to look in that directory for driver files.
3. Plug device in.
Windows will ask for driver files.
4. Choose to install automatically.
Windows will use stored directory (2) to install drivers.
My Problem is step (1). What I tried was:
- include the driver files in a subdirectory of my application project (Right click on project, "Add", "New folder")
- add "Content files" to setup's file system view (Or whatever "Inhaltsdateien" reads in english).
- compile and execute setup on target machine.
Some of the files were copied, but others were not. Here are the files I had added to the application project:
- USBDriver
- amd64
| \ ftbusui.dll
| \ ftcserco.dll
| \ ftd2xx.lib
| \ ftd2xx64.dll
| \ ftdibus.sys
| \ ftlang.dll
| \ ftser2k.sys
| \ ftserui2.dll
- i386
| \ ftbusui.dll
| \ ftcserco.dll
| \ ftd2xx.dll
| \ ftd2xx.lib
| \ ftdibus.sys
| \ ftlang.dll
| \ ftser2k.sys
| \ ftserui2.dll
- Static
| - amd64
| | \ ftd2xx.lib
| - i386
| \ ftd2xx.lib
\ DPInst.exe
\ dpinst.xml
\ ftd2xx.h
\ ftdibus.cat
\ ftdibus.inf
\ ftdiport.cat
\ ftdiport.inf And these are the files that got copied:
-USBDriver
- amd64
| \ ftbusui.dll
| \ ftcserco.dll
| \ ftd2xx64.dll
| \ ftlang.dll
| \ ftserui2.dll
- i386
| \ ftbusui.dll
| \ ftcserco.dll
| \ ftd2xx.dll
| \ ftlang.dll
| \ ftserui2.dll
\ DPInst.exe
\ dpinst.xml
\ ftd2xx.h
\ ftdibus.inf
\ ftdiport.inf
It seems that files with extensions .sys, .cat and .lib did not get copied.
Why is that?
How can I transfer all of the driver files to their destination?
Ciao,
luker
|
|
|
|
|
Everything you need should be in this[^].
|
|
|
|
|
Unfortunately, this doesn't help me.
The paper describes how to create a driver package. I already have a ready-to-install driver package and am trying to include it in a setup project.
The setup project doesn't include all files that I told its application to include. What to do about that?
Ciao,
luker
|
|
|
|
|
Actually, you'd be repackaging your drivers to put the files in the appropriate locations.
|
|
|
|
|
Hi every body
I have Q
how read/write List OF DataRaw to network Stream
please I want answer Soonly
And thanks
|
|
|
|
|
Please read the "How to get an answer to your question" topic on top.
-> Elaborate on what you're trying to achieve
-> Take time to formulate your question and write full sentences.
-> Indicate what the (isolated) problem is and what you have done to solve the problem yourself.
Example:
If you seek a starting point for information on a project you can always ask the question like - I want to do a project on topic X and have search google/Codeproject/ site whatever on these keyword, but results are insufficient. Could you please point me to some good resources on where to start.
hope this helps in getting the answer you seek.
V.
|
|
|
|
|
DataTables (and I think DataViews) can be serialised to XML, which can obviously be sent down a stream. Package your DataRows up in one of them and get the XML serialisation.
|
|
|
|
|
om_metab wrote: please I want answer Soonly
You get answers when people are available to give them. You've asked enough questions by now to know that adding comments like this does nothing to increase the urgency with which people reply to you.
As for your question, what have you tried? What technology are you using? WCF? Custom written TCP?
|
|
|
|
|
|
Hello,
Most code used to detect USB relies on a Windows form. The program must register its interest in the USB insertion/removal events by sending its main window handle to RegisterDeviceNotification. What I'm trying to do is make a class library which exposes DeviceIsConnected property without having to use any forms. Any idea if this can be done, and if so how?
Thanks!
Mart
|
|
|
|
|
I would use WMI for that task. How To: (Almost) Everything In WMI via C# - Part 3: Hardware[^].
Example:
WqlEventQuery query = new WqlEventQuery(eventName,
new TimeSpan(0, 0, 1),
"TargetInstance isa \"Win32_USBControllerDevice\"");
EventWatcher = new ManagementEventWatcher();
EventWatcher.EventArrived += eventHandler;
EventWatcher.Query = query;
|
|
|
|
|
Thanks Kim! I went through the WMI hardware tutorial and was able to run the sample code, but it listed every piece of hardware in the PC except for the one I was interested in, i.e. a USB Input Device under Human Interface Devices. The HID device has this ID: USB\VID_0000&PID_0000&REV_0100. Any idea why I can't communicate with the device using WMI?
Thanks again,
Mart
|
|
|
|
|
hello guys.. I want to copy a list which contains key/values. Now I am unable to sort that list, so I thought to copy that list into KeyValuePair() and the sort this. Here is the list
List<Students> list = students.ToList<Students>()
Now I want to copy this into KeyValuePair(). How can I do that? thnx
|
|
|
|
|
You can't, per-se, copy a list into a KeyValue pair because you are missing the key part of the KVP. You're going to have to iterate over the list and add it into the KVP one value at a time (using an appropriate element to create the key).
|
|
|
|
|
Students already contains name value pairs like name, degree. I am not able to get the idea how to sort that list which already contains name/values pair.
|
|
|
|
|
So, you have a list of Students (btw, your class should really be called Student as it maps to a single student) and you want to sort on the data, e.g. the student Name. What you could do, using Linq for instance:
var output = students.OrderBy(student => student.Name);
|
|
|
|
|
|
If the only reason you want to put it in a KVP is to sort it, you'd be better off having Students (by the way, it should probably be 'Student' unless that class is itself a wrapper for a group of students) implement IComparable and just calling List.Sort. (Or, if you don't own Students, writing a IComparator<Students>.)
|
|
|
|