|
To add to this (for the original poster), a more common data-binding example is to bind against a DataSet , or a business object like I mentioned. Either one could be filled from a data source using the provider pattern so that the implementation is really independent from the data. This is a good OO design.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
After I add a row to a dataset how can I update that same row in the dataset after it has been added?
Thanks,
JJ
|
|
|
|
|
I was thinking the LoadDataRow method but this querys the Primary key for row which doesn't exist yet since generated by the database when records from dataset gets inserted. Or maybe I can't use an autoincrement field in the database table anymore because I have to supply one for new row inserts into dataset? Any Ideas?
Thanks,
JJ
|
|
|
|
|
If you set up your DataSet correctly with auto-increment columns and DataRelation s between then, the SqlDataAdapter should take this into account automatically, which is one reason that most InsertCommand properties use a SqlCommand comprised of an INSERT followed by a SELECT (separated by a semi-colon). Another reason is sometimes there may be other auto-generated fields that are optional, like time stamps.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
ok I usually use commandbuilder and set it to my SqlDataAdapter but not sure about what you are saying with the UPDATE followed by a SELECT(separated by a semi-colon). I don't believe I have seen it done this way yet. Can you give me an example?
Thanks,
JJ
|
|
|
|
|
Use something like the following for your SqlCommand you set as the SqlDataAdapter.InsertCommand :
INSERT INTO MyTable Name VALUES('Name'); SELECT * FROM MyTable; The SELECT statement should actually match the SQL statement you use for your SelectCommand .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
When you called NewRow , you got a DataRow . After you add that, the object reference has not changed. If you don't have that variable that references the DataRow anymore (like it fell out of scope), then you'll have to enumerate your DataTable to find it, or use DataTable.Select with a filter expression (see DataColumn.Expression for syntax) to find it.
This is all well-documented in the class documentation for DataSet , DataTable , and DataRow classes, which you really should read.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how do i plot a graph that looks like the CPU load monitor graph in XP?
I just need the formulae or some code that shows how to do it. I have the data available , and I could easily find my way to plot the graph using the graphics libs.
I'm guessing , this is going to be using some Gaussian noise formulae. But google didnt give me any affirmitive results.
Any ideas ?
|
|
|
|
|
|
A Windows user with regular "user" permissions is running my C# program, and it's not able to access HKEY_LOCAL_MACHINE/Software/Blah
Does that sound right?
What are the permissions rules for HKEY_LOCAL_MACHINE?
Thanks,
Elena
|
|
|
|
|
By default, everyone has permissions to read HKLM, and that's the way it should be. Users should only be able to write changes to their hive (HKEY_CURRENT_USER). If you have user-specific settings, that's where they should go.
Actually for .NET applications, nothing should go in the registry in a typical scenario. You typically use the .config file for application settings. If you have user settings, you can save them in a user's isolated storage (see the System.IO.IsolatedStorage namespace in the .NET Framework SDK). This allows for touchless deployment, or XCOPY deployment as it's sometimes called.
At the very least, your application should be sensitive to the fact that settings in the registry might not exist, using default values or displaying user-friendly errors where appropriate.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am trying to read HKLM, and it's not letting me.
Elena
|
|
|
|
|
If you're getting a specific problem, be specific. What exception is being thrown? If you're handling the exception, then break into your debugger and tell us. "it's not letting me" tells me nothing.
Also, see my other post. If this code is not running from the local machine (or the CAS user policy is more limited), code access security may be preventing the call to the registry class methods.
Finally, I told you what the default permissions are. If you want to know what they are on your machine, then open regedit.exe, right-click on HKEY_LOCAL_MACHINE and select Permissions (if you don't see that, use regedt32.exe instead since this was a new UI feature in recent Windows OSes).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Oh, and one more thing: depending on the source of the executing app, code access security may be preventing calls as well. If the application is running from the Intranet zone, it's granted limited permissions (no registry access at all). For the Internet zone, either no permissions are granted (.NET 1.0) or very few are granted (.NET 1.1).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have created a user control, on which I have place an MDI Client control. I next added my control to a project. Now, I want to open new Forms inside of the Client. Here is my problem:
childForm myChild = new childForm();
mdi1.m.Controls.Add(myChild);
<br />
myChild.Show();
The error occurs here:
<br />
mdi1.m.Controls.Add(myChild); <br />
this is the error:
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Can only add MDI child forms to an MdiClient.
The only way I can currently get around this is to make my main form an MDI Parent, and add this code before adding the form to the user control:
<br />
myChild.MdiParent = this; <br />
Then, when I try to add the form to the user control, it works beautifully. I can only assume that this is because the Form.IsMdiChild property is set to TRUE.
But, I need to do this without first adding it to an MDI Parent (making my main form an MDI Parent.)
So my question is:
1) Is there a way to write to the Form.IsMdiChild value?
or
2) Is there another way to do this?
Hopefully, this problem will interest somebody... Thanks.
Agent 86
|
|
|
|
|
Agent 86 wrote:
1) Is there a way to write to the Form.IsMdiChild value?
Nope. This is a read only property settable at design time only.
Agent 86 wrote:
2) Is there another way to do this?
Nope. You can't have an MDI Parent be a child of another MDI Parent, which is what you are trying to do. A form can either be an MDI Parent OR an MDI Child, but not both at the same time. There is no way around this...
RageInTheMachine9532
|
|
|
|
|
Hi,
I'm trying to create a dataset with an xsd file but get a 'System.InvalidOperationException' when I try to fillSchemain DataAdapter. It's prbably failing because I didn't set a connection to Dataadapter but I don't have a connection to database because this is offline. How can I get the Schema into the dataset? Here's my code:
<br />
DataSet IpacDS = new DataSet("Midnight"); <br />
<br />
try<br />
{<br />
IpacDS.ReadXmlSchema(sXmlFileName);<br />
SqlDataAdapter da = new SqlDataAdapter();<br />
da.FillSchema(IpacDS, SchemaType.Source); <-- Fails here<br />
<br />
<br />
<br />
<br />
|
|
|
|
|
Why are you even trying to do this? FillSchema builds the schema for the DataSet by using the SelectCommand . If you've already got your schema (referenced by sXmlFileName ) and are reading that into the DataSet using ReadXmlSchema , that's all you need to do. Just make sure that schema and what you'd pull from the database (yes, which requires a connection otherwise the schema can't be determined) are the same (or that the latter is a subset of the former).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I solved my problem. If I name the table in the Dataset as something other than MN, it would fail on the line I mention. Why is that so?
Here is the code I used:
<br />
<br />
DataSet IpacDS = new DataSet("MN"); <br />
<br />
try<br />
{<br />
IpacDS.ReadXmlSchema("MN.xsd");<br />
DataRow nRow = dsDiff.Tables["MN"].NewRow(); <- Would Fail here if I didn't name it MN.<br />
<br />
<br />
Any ideas why ?
Thanks,
JJ
|
|
|
|
|
Because no DataTable would be found named "MN", so dsDiff.Tables["MN"] would return null . You can't call NewRow() on null , hence the NullReferenceException you would've received. This is the result of improper checks and exception handling.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
i'd like to sort the child items of a node of a Windows Forms TreeView control using a function of my own that set if the node is to be listed before or after another. Every node has a logic code that i've stored internally using a derived class of TreeNode and i'd like to sort the item by this one.
In MFC there's the possibility of defining a logic compare function, is there anything similar in .NET? Or there's another way to add a new child node of an item and sort all its childs without removing them and reloading ?
Thanks,
Gianmaria
|
|
|
|
|
In the TreeView class, there is no such property or method for you to provide your own sorting algorithm. The Tree-View common control - which the TreeView class encapsulates - does, however. See the documentation for the TVM_SORTCHILDRENCB message in the Platform SDK.
You can fill TVSORTCB struct (which you must create in C#) with data from the parent tree node (use TreeNode.Handle ), a callback function (shouldn't need to be pinned using the GCHandle since the SendMessage call would be synchronous), and any other data you want to pass (like an integer value that specifies whether to sort ascending or descending).
The struct and callback would look like this:
[StructLayout(LayoutKind.Sequential)]
private struct LVSORTCB
{
public IntPtr TreeItemHandle;
public TreeViewSortCallback Callback;
public IntPtr ExtraData;
}
public delegate IntPtr TreeViewSortCallback(IntPtr node1, IntPtr node1,
IntPtr extraData); The return Type is an IntPtr because an unmanaged int is processor-dependent (32 bits on a 32-bit proc, 64 bits on a 64-bit proc). This makes it more portable.
To call it, you need to P/Invoke SendMessage :
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int msg,
IntPtr wParam,
ref TVSORTCB sort);
public void Sort(TreeNode parent, TreeViewSortCallback callback, bool ascending)
{
TVSORTCB sort = new TVSORTCB();
sort.TreeNodeHandle = parent.Handle;
sort.Callback = callback;
sort.ExtraData = new IntPtr(ascending ? 0 : 1);
IntPtr hWnd = treeView1.Handle;
SendMessage(hWnd, 4373, IntPtr.Zero, ref sort);
} Create a new TreeViewSortCallback delegate that references your function and call such a method. You can use your sorting algorithms in there.
If you want to get really fancy, you could encapsulate this all into a derivative TreeView class, perhaps called SortableTreeView or something, and let the caller pass an IComparer implementation. With a good design, you could couple the interface (common throughout the .NET FCL) with the callback (making the callback private since IComparer is the typically sorting implementation in .NET - consistency is important).
Of course, you could always use your algorithms and manually move nodes around, but then you're responsible for all the work. Using the approach above makes the Tree-View common control do it with very little information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks for the infos. i've implemented a sort function like this in my TreeView class:
<br />
public IntPtr Compare(IntPtr node1, IntPtr node2, IntPtr extraData)<br />
{ <br />
TreeNode node_1 = TreeNode.FromHandle(this, node1);<br />
TreeNode node_2 = TreeNode.FromHandle(this, node2);<br />
IntPtr ret= new IntPtr(0);<br />
long prog1 = ((myTreeNode)node_1).item_type;<br />
long prog2 = ((myTreeNode)node_2).item_type;<br />
if(prog1 < prog2) { ret = new IntPtr(-1); }<br />
if(prog1 > prog2) { ret = new IntPtr (1); }<br />
return ret;<br />
}<br />
and call the sort function
Sort(parent_node, new TreeViewSortCallBack(Compare), 0);
at that point, the Compare function is called but both node1 and node2 are zero valued. is because those values are not the nodes but the ItemData like in MFC?
|
|
|
|
|
As I mentioned before (and that is also documented for the TVSORTCB struct, which you should read), the first two params are the TVITEM structs. You must declare the TVITEM struct in your code and either use Marshal.PtrToStructure , or better yet declare the delegate as:
delegate IntPtr CompareFunc(ref TVITEM item1, ref TVITEM item2, IntPtr userData); This causes the CLR to marshal the references to the TVITEM structs so you don't have to. You then can get the TreeNode s using the TVITEM.hItem fields (or whatever you call the second field in the TVITEM struct, which is the HTREEITEM (handle to a tree node).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i've tried in the 2 ways, with ref TVITEM and Marshal but it doesnt work. the first two parameters are always 0 or undefined (using ref TVITEM).
i've declared the structure
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct TVITEM<br />
{<br />
public IntPtr mask;<br />
public IntPtr hItem;<br />
public IntPtr state;<br />
public IntPtr stateMask;<br />
public IntPtr pszText;<br />
public IntPtr cchTextMax;<br />
public IntPtr iImage;<br />
public IntPtr iSelectedImage;<br />
public IntPtr cChildren;<br />
public IntPtr lParam;<br />
} <br />
<br />
my second test was with
<br />
public IntPtr Compare(IntPtr node1,IntPtr node2, IntPtr extraData)<br />
{<br />
TVITEM item1 = new TVITEM(); Marshal.PtrToStructure(node1, item1);<br />
TVITEM item2 = new TVITEM(); Marshal.PtrToStructure(node2, item2);<br />
TreeNode node_1 = TreeNode.FromHandle(this, item1.hItem);<br />
TreeNode node_2 = TreeNode.FromHandle(this, item2.hItem);<br />
but always the first 2 parameters are undefined. I've checked the value of extraData and it's correct, the same value i pass when call the sorting. I cant understand why the handle values of the 2 nodes are not passed to the callback function. However forcing the sorting of the two nodes, the tree updates correcly. But there's no way of testing the values of the 2 nodes.
I've created a little test application with only these things, and the behaviour is indentical
|
|
|
|
|