|
hello guys... I have this datagridview in which I show students. Now when I add more than one students in this grid and update their data, it shows me this error
<pre>Object Can not be Bast From DBNullto Other Types</pre>
Here is the code I am using to loop through and update these students from gridview
<pre lang="c#">
foreach (DataGridViewRow row in dgStudents.Rows)
{
object obj = row.Cells["colStudentId"].Value;
if (obj != null)
{
StudentID = Convert.ToInt32(row.Cells["colStudentId"].Value);
Fee = Convert.ToInt32(row.Cells["colFee"].Value);
NumOfSubject = Convert.ToInt32(row.Cells["colNumOfSubject"].Value);
}
}
</pre>
Even though I check for null values taken from gridview, it still shows me this error. Whats wrong? thnx
-- modified 4-Jan-12 1:04am.
|
|
|
|
|
try this:
if(row.Cells["colStudentId"].Value != DBNull.Value){
...
}
hope this helps.
V.
|
|
|
|
|
Thnx for your time. Actually I had to check all numeric types against null, in the grid.
Then it did not happen again.
|
|
|
|
|
Plus, Int32.Parse and Int32.TryParse are preferable to Convert.ToInt32 -- dont use Convert.
|
|
|
|
|
how to call the database to be displayed,,
through programming asp.net with c#,,
because of problems that came out at compile time
myLayer.DataSource = new SharpMap.Data.Providers.katmai (ConnStr, "dbo.sysdiagrams", "dbo.Geocode", "OidColumn");
please give an example at all,,,
thank you
modified 2-Jan-12 21:08pm.
|
|
|
|
|
I'm sure English is not your native language, but it is the default language for this site.
In English, your question makes no sense at all.
Please, either try to find a better translation of your question to English, or find a site in your own native language, as they may be able to help you better than we can!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
hello friends I work for several months in my hand Makel bi business counters and Kohler have not comport with the remote trying to read them in c # la. and the cable will be connected to a one count. I would say that bi does not like to read one of a few sayacdan. but please do not get the help you lead a more bi olurmusunuz How do I make this work. I give all the settings of the timer in visual studio, how will I am using the SerialPort object. I need to send data to send data to the meter data to come to the counter where the error do you think I give below the meter does not respond to me. If you help too sevinirim. Thank you in advance
string command;
command ="/?!" (char) 13 (char) 10;
byte [] array;
Array = Encoding.UTF8.GetBytes (command);
serialPort1.Write (array, 0, array.length);
|
|
|
|
|
I suggest you start by reading the recent series of three articles on C# serial port programming by Pantelis Chatzinikolis, here on CP:[^].
I also suggest you ask a friend whose English skills are good to help you re-write your question, so it's clearer.
Be sure and tell;
1. the exact nature of the hardware device to which you send information out to by the serial port.
2. does the device that receives information from the serial port of the "transmitter" communicate anything back to the device that transmits: if so, what is that information sent back: and what is the device that receives that information: a server on a network, an individual PC ?
good luck, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
i have written the code for filling a treeview with some nodes and subnodes by enabling checkboxes. when a subnode is checked its parent should be checked and when a parent is checked all of its childs should be cheked(like general scenario).
But when i started copying the selected nodes in the treeview1 to new treeview(treeview2)i am getting so many problems. i tried different events to achieve this like NodeMouseClick,treeviewAfterSelect,Aftercheck,AfterExpand but unable to get the required node set (total hierarchy of checked nodes with out repetition and should reflect every change showing and shoud be removed when unchecked )
can any one please help in doing this ?
janardhan
|
|
|
|
|
If you change the way that you think about the problem a bit, you'll find that there's a relatively simple way to solve the problem. Currently it sounds like you are relying on navigating over the view to work out what has been checked, and you are using that to actually perform the update. Now, this is one potential approach, but it's a pretty fragile mechanism.
Rather than approaching the problem like this, why not use an underlying model and use that to control the update? This is the approach that is favoured in developments using WPF, Silverlight and ASP.NET MVC.
|
|
|
|
|
Thanks alot for the reply .You are correct i am relying on navigating on view but i am using windows forms application and i cant use wpf and other environments thats my problem.
|
|
|
|
|
I wasn't suggesting that you change platforms. What I was suggesting is that you could use a pattern such as MVC, and leverage data binding to trigger the update.
|
|
|
|
|
Ok i am new to c sharp i dint used databinding concept. but i will try out databinding to trigger the update .thank you
|
|
|
|
|
It would be good to know if you are using WinForms, WPF, or ? here.
In this case we don't much about the structure of your TreeView: can it have multiple levels of nesting ?
If it can have multiple levels of nesting, and a "child node" three levels deep has its CheckBox checked, then you tell us that all that node's "sibling nodes," and its "parent node" are to have their CheckBoxes checked: but ... then does the parent of the parent node also get all its child nodes, and itself checked ?
Or, is the (as I would assume) "checking" only of the immediate sibling nodes, and the parent.
What happens if the user then unchecks one of the sibling nodes: does that then uncheck all the other sibling nodes ... and the parent ? If the parent node is unchecked: all the sibling nodes are then unchecked ?
Assuming you allow unchecking in the first TreeView: you then need to alter the second TreeView ? ... I'd guess: "yes."
In any case, you can populate a new TreeView by recursively parsing the existing TreeView and creating new Nodes that match those that are checked in the second TreeView, with the same parent/child relationships.
Is recursive parsing of a TreeView something you know how to do now, or can we help with you that ?
And, if your TreeView is only two levels "deep:" a set of root nodes, and each root node having a set of child nodes: then this becomes very easy.
So, some more information about the structure of your TreeView would be helpful.
best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
Hi BILL, THANKS ALOT for the reply !!
I am using windows forms application.It can have multiple levels of nesting.
If a node at 3 levels deep is checked only its parent and grand parent are to be checked but not it siblings.
If the user unchecks the a node it has no effect on its siblings but if its parent has no other childs except it then that parent also should be unchecked.
If the parent is unchecked all its childs should be uncheked.
If a node in the first treeview is unchecked it should reflect immeditely in the second treeeview.
Can you help in recursive parsing of treeview you are saying or with any other solution.
|
|
|
|
|
Well, we are making progress in defining the actual structure of your TreeView ... and that's good
This reply raises these questions, for me:
0. is any node in the TreeView ever more than 3 levels deep: in other words: is the level of "nesting" arbitrary ? In other words: is there any regular structure to all nodes that we can assume: for example: they are all 3 levels "deep" ?
1. in the case of one node, which is the only child of its parent: you say the parent should also be unchecked: what about if that parent node is also the "only child" of its parent node: does its "grandfather" then get unchecked ?
a. what if the grandparent node of the only child of the parent node that is unchecked has many other child nodes ... "uncle nodes" ? ... what happens to them: do they get left alone, or unchecked, also ?
2. if a parent node is unchecked: and that parent node has many levels of nested nodes "under it:" do they all get unchecked ... or only the "grandchildren" of that node get unchecked: this would be "symmetric" with the way you now handle unchecking an only child of a node, then its parent, and then its grandparent.
2. is it ever the case that:
a. new nodes are added at run-time ?
b. existing nodes are deleted at run-time ?
To get you started with recursive parsing: put a TreeView on a Form:
0. in the Form Load EventHandler set the 'Expanded property of the TreeView to 'true.
1. edit the Nodes collection at Design-Time, and add a whole bunch of them, nested to various depths
2. set the TreeView 'CheckBoxes property to 'True:
3. put a button on the Form with its Click EventHandler set like so:
private void button1_Click(object sender, EventArgs e)
{
treeView1.ExpandAll();
RecursiveParseTree(treeView1.Nodes, 0);
} 4. And then define the recursive parser:
private void RecursiveParseTree(TreeNodeCollection nodeCollection, int level)
{
foreach (TreeNode theNode in nodeCollection)
{
Console.WriteLine
(
"Node Name: = {0},
Node Level = {1},
Node Checked = {2}",
theNode.Text,
level,
theNode.Checked
);
if (theNode.Nodes.Count > 0)
{
RecursiveParseTree(theNode.Nodes, level++);
}
}
} 5. At run-time check a bunch of nodes at various "depths," and examine the output of running this code in VS's Output Window when you exit the Application.
Discussion: the interesting part is going to be where you get to all the business related to unchecking only-child nodes, and grandparent nodes !
best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
modified 3-Jan-12 5:25am.
|
|
|
|
|
i will answer your questions and i will give the code i wrote for checking subchilds so that it will be more clear for you.
0.Actually nodes in my treeview or folders and its subfolders are childs and sub subfolders are grand childs.when a user gives the folder by folderbrowser dialog i am capturing its path and extracting all its folder hierarchy and showing it in the treeview. so tree can be of any depth.
1.in the case of one node, which is the only child of its parent: you say the parent should also be unchecked: what about if that parent node is also the "only child" of its parent node: does its "grandfather" then get unchecked ?
-yes
a. what if the grandparent node of the only child of the parent node that is unchecked has many other child nodes ... "uncle nodes" ? ... what happens to them: do they get left alone, or unchecked, also ?
- uncle nodes state remains the same, only its parent node (if it is only child for it) is unchecked if it is unchecked.if it is checked obviously its parent node should be checked wether it has other childs or not.
2.if a parent node is unchecked: and that parent node has many levels of nested nodes "under it:" do they all get unchecked ... or only the "grandchildren" of that node get unchecked
-all the nodes shoould be unchecked
2.a. new nodes are added at run-time ? not added but checked or unchecked
b. existing nodes are deleted at run-time ?no not deleted but checked or unchecked.
hope this code will clarify your doubts :
private void Load_treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode parent_node = e.Node;
bool is_checked = parent_node.Checked;
for (int i = 0; i <= e.Node.Nodes.Count - 1; i++)
{
SetSubtreeChecked(parent_node.Nodes[i], is_checked);
}
bool blnUncheck = false;
if ((e.Node.Parent != null))
{
foreach (TreeNode child in e.Node.Parent.Nodes)
{
if (child.Checked == true)
{
blnUncheck = true;
}
}
if (blnUncheck == false)
{
e.Node.Parent.Checked = false;
}
else
{
e.Node.Parent.Checked = true;
}
}
}
private void SetSubtreeChecked(TreeNode parent_node, bool is_checked)
{
parent_node.Checked = is_checked;
for (int i = 0; i <= parent_node.Nodes.Count - 1; i++)
{
SetSubtreeChecked(parent_node.Nodes[i], is_checked);
}
}
the recursive parsing code u gave looks solves the problem i will try that.Thank you
JANARDHAN
|
|
|
|
|
thanks alot Bill
it solved the problem
can i call this RecursiveParseTree() in any of the events of the treeview1 or 2 so that i need not click the button??
JANARDHAN
|
|
|
|
|
JANARDHAN GURRAM wrote: can i call this RecursiveParseTree() in any of the events of the treeview1 or 2 so that i need not click the button?? Yes, you can call it from anywhere, and you can recurse on the whole TreeView, or on any Node in any TreeView.
Whether you'd really want to have a different recursive parser for the second TreeView compared to the first one: well, that's a design problem you'll need to figure out depending on the difference in the way you alter/edit/use the two TreeViews.
Actually, even if you called it on a Node that had no child nodes: you'd pass a TreeNodeCollection whose 'Count property was #0 into the recursive procedure, and it would exit without error.
So, if you are using this recursive parsing on Nodes within the TreeView, you might want to "wrap the call" in something like this:
TreeNode targetNode = ????;
if(targetNode.Nodes.Count > 0)
{
RecursiveParseTree(targetNode.Nodes,0);
}
else
{
} p.s. if answers here help you: please vote: if they didn't help you: please vote. imho, feedback strengthens our whole community.
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
Bill your solution is obsolutely correct and it worked for me..but if there are 50 k nodes like in my case for every little change parsing whole treenodecollection is time taking . so what i did i have sent only checked nodes to the recursiveparsetree and there i have added them to string array and deleted from it when modified and used it for 2nd treeview(actually i need to display in table..wat ever it may be).
private void Load_treeView_AfterCheck(object sender, TreeViewEventArgs e)
{
RecursiveParseTree(e.Node, 0);
}
private void RecursiveParseTree(TreeNode thenode, int level)
{
if(thenode.Checked==true)
{
foreach(TreeNode childNode in thenode.Nodes)
if (childNode.Checked == true)
{
_dataArray.Add(new string[] { thenode.Text, childNode.Text });
}
}
if (thenode.Checked == false)
{
foreach (TreeNode childNode in thenode.Nodes)
{
List<string> Key = new List<string> { thenode.Text, childNode.Text };
_dataArray.RemoveAll(item => Enumerable.SequenceEqual(item, Key));
}
}
}
<div class="signature">JANARDHAN</div>
|
|
|
|
|
Glad you are making progress on your solution !
Obviously, you've come up on the issue of performance, and here a few comments I hope you will find useful:
By the way: have you explored all the possible uses in your scenario of TreeNode.Clone() ... which .... from MSDN: "Copies the tree node and the entire subtree rooted at this tree node." ... ?
0. is there any way you can limit the update to some user-initiated update (i.e., make it a batch operation), which of course may mean you'd have to tolerate a mis-match visually between the two TreeViews, assuming both are displayed at the same time.
If you can "get away with that," then I'd disable the second TreeView anytime it was not synchronized, after some change had been made in the primary TreeView, but the user had not initiated an update.
1. however, if it is an absolute requirement that checking any TreeNode results in immediate changes in the second TreeView: then there are several ideas to explore:
a. since you can get an Event for any TreeNode CheckState changed: and having access to the changed Node also gives you access to its Parent property: does that give you enough information to do a partial update ?
b. whether the FullPath property of a TreeNode could be useful here ? I doubt it, since the WinForms TV gives you no useful methods to use that FullPath to directly get at TreeNodes (by, for example, using only part of the FullPath returned string).
2. view virtualization (showing only what you need to show as updated): I think there may be an example on here on CP of a TreeView supporting "virtual screen update" which means changes are made as they are required to be displayed: while I doubt that's relevant here, at least you might check it out ... for ideas.
3. flatlist: the third-party commercial TreeView I use (Lidor Systems IntegralUI TreeView) maintains a flattened list of all Nodes in depth-first order, which makes operations like the kind of updates you are looking at very much easier: and many other features including "virtualization of view." The WinForms TreeView, by comparison, is a "toy." Yes, that's a recommendation ... and no I don't work for Lidor.
a. on StackOverFlow you'll some good examples of how to create a flattened TreeView List of Nodes, based on ideas of Eric Lippert influenced by ideas of Wes Dyer: the method uses a stack to avoid the high performance/memory costs of true recursion. Check out this thread I started on SO a long time ago: [^].
4. finally, if immediate synchronization of both views here is the goal: re-consider Luc Patyn's suggestion to use your own tree data structure, and map it as needed to "views."
good luck, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
i called it in after check event of treeview1 it worked with out need for button.
thanks alot BILL
JANARDHAN
|
|
|
|
|
Hi all,
I program a Snake game using C#.
I have a problem when I moving the snake.
I have a while loop to made the snake move while it's not dead.
Here is the code:
while (!mainSnake.isDead)
{
mainSnake.makeMove();
PicCanvas.Invalidate();
Thread.Sleep(500);
}
The problem is that I don't see the moves of the snake. I just see the "Game Over" mesagebox when the snake "hit the wall".
I want to see any move of the snake (with 500 millisecond delay between any move).
Any ideas?
Thanks ahead.
|
|
|
|
|
I guess the code shown sits in some event handler, i.e. it is executing on the main thread and blocking everything else, until it terminates due to the snake dying. This is what you should do:
- get rid of Thread.Sleep(), it should not be used inside event handlers.
- use a timer instead; for actions that mainly influence the GUI, a System.Windows.Forms.Timer is the right choice.
- put one iteration of your loop inside the timer's tick handler.
|
|
|
|
|
Thank you the timer really worked!
|
|
|
|
|