Refreshing data on a parent form at child close
-
I've worked a lot to try to get this working, but after several errors and trials, i've tried two things without error, but it doesn't work here is my customer list load code
SQLiteParameter\[\] param = {}; // MessageBox.Show("ran?"); customerList.Rows.Clear(); DataTable searchinfo = database.query\_search("SELECT id, firstname, lastname FROM customers", param); foreach (DataRow row in searchinfo.Rows) { customerList.Rows.Add(row\[0\].ToString(), row\[1\].ToString() + " " + row\[2\].ToString()); } statusLabel.Text = searchinfo.Rows.Count.ToString() + " row(s) returned";
double clicking or clicking an 'edit' button successfully raises a child form
private void customerList\_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int index = e.RowIndex; DataGridViewRow row = customerList.Rows\[index\]; String cid = row.Cells\[0\].Value.ToString(); editcustomer editdiag = new editcustomer(); editdiag.id = cid; editdiag.ShowDialog(); }
I used a timer to filter results from a textbox that starts and stops based on textchanged
private void search\_TextChanged(object sender, EventArgs e) { if(queryInterval.Enabled == false) queryInterval.Start(); }
here is the 1000ms tick that stops itself after an idle match
public void queryInterval\_Tick(object sender, EventArgs e) { if (lastQuery == search.Text) { queryInterval.Stop(); } else { if (search.Text.Trim() != "" && search.Text.Length >= 3) { customerList.Rows.Clear(); SQLiteParameter\[\] param = { new SQLiteParameter("@search", "%" + search.Text + "%") //used direct string for debug //search.Text) }; DataTable searchinfo = database.query\_search("SELECT id, firstname, lastname FROM customers WHERE firstname LIKE @search OR lastname LIKE @search", param); foreach (DataRow row in searchinfo.Rows) { customerList.Rows.Add(row\[0\].ToString(), row\[1\].ToString() + " " + row\[2\].ToString()); }
-
I've worked a lot to try to get this working, but after several errors and trials, i've tried two things without error, but it doesn't work here is my customer list load code
SQLiteParameter\[\] param = {}; // MessageBox.Show("ran?"); customerList.Rows.Clear(); DataTable searchinfo = database.query\_search("SELECT id, firstname, lastname FROM customers", param); foreach (DataRow row in searchinfo.Rows) { customerList.Rows.Add(row\[0\].ToString(), row\[1\].ToString() + " " + row\[2\].ToString()); } statusLabel.Text = searchinfo.Rows.Count.ToString() + " row(s) returned";
double clicking or clicking an 'edit' button successfully raises a child form
private void customerList\_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int index = e.RowIndex; DataGridViewRow row = customerList.Rows\[index\]; String cid = row.Cells\[0\].Value.ToString(); editcustomer editdiag = new editcustomer(); editdiag.id = cid; editdiag.ShowDialog(); }
I used a timer to filter results from a textbox that starts and stops based on textchanged
private void search\_TextChanged(object sender, EventArgs e) { if(queryInterval.Enabled == false) queryInterval.Start(); }
here is the 1000ms tick that stops itself after an idle match
public void queryInterval\_Tick(object sender, EventArgs e) { if (lastQuery == search.Text) { queryInterval.Stop(); } else { if (search.Text.Trim() != "" && search.Text.Length >= 3) { customerList.Rows.Clear(); SQLiteParameter\[\] param = { new SQLiteParameter("@search", "%" + search.Text + "%") //used direct string for debug //search.Text) }; DataTable searchinfo = database.query\_search("SELECT id, firstname, lastname FROM customers WHERE firstname LIKE @search OR lastname LIKE @search", param); foreach (DataRow row in searchinfo.Rows) { customerList.Rows.Add(row\[0\].ToString(), row\[1\].ToString() + " " + row\[2\].ToString()); }
Your problem is to do with the way you display the child form: ShowDialog is a modal function: it does not return until the form it displays is closed. So the UI for Form1 is "busy" and doesn't get updated while the child form still exists. Instead of ShowDialog, you can use Show - which allows both forms to continue working - but in any case you are going at this the wrong way: 1) Creating a new instance of the "parent" form and calling it's methods will not affect the existing one: it's a separate instance and does not share a DataGridView with any other. In order to directly update the parent, you would need to use the actual instance that opened the child, not a new one. 2) But the child form shouldn't even know the parent form exists, much less what type it is, or what methods it contains. Instead, the child should raise events that the parent handles and let teh parent do what it wants with the updates. That probably sounds complicated, but it actually solves both your problems very neatly. Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] It will show you how to do it properly.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Your problem is to do with the way you display the child form: ShowDialog is a modal function: it does not return until the form it displays is closed. So the UI for Form1 is "busy" and doesn't get updated while the child form still exists. Instead of ShowDialog, you can use Show - which allows both forms to continue working - but in any case you are going at this the wrong way: 1) Creating a new instance of the "parent" form and calling it's methods will not affect the existing one: it's a separate instance and does not share a DataGridView with any other. In order to directly update the parent, you would need to use the actual instance that opened the child, not a new one. 2) But the child form shouldn't even know the parent form exists, much less what type it is, or what methods it contains. Instead, the child should raise events that the parent handles and let teh parent do what it wants with the updates. That probably sounds complicated, but it actually solves both your problems very neatly. Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] It will show you how to do it properly.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
lol I actually ran across that page during my research. It sounds real complicated. a little blood just dropped from my nose
-
lol I actually ran across that page during my research. It sounds real complicated. a little blood just dropped from my nose
It's not difficult at all, not really - you've got the full code, and all it's doing is using the same event mechanism that everything else does, just you are creating and raising the event as well as handling it. And creating an event is almost trivial, I do it so often I created a Visual Studio shortcut to to make it even simpler: A Simple Code Snippet to Add an Event[^]. Now I just type "evh" and press TAB and it fills out the event, just like "prop"+TAB creates a property. Raising the event is just a case of calling a method!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
I've worked a lot to try to get this working, but after several errors and trials, i've tried two things without error, but it doesn't work here is my customer list load code
SQLiteParameter\[\] param = {}; // MessageBox.Show("ran?"); customerList.Rows.Clear(); DataTable searchinfo = database.query\_search("SELECT id, firstname, lastname FROM customers", param); foreach (DataRow row in searchinfo.Rows) { customerList.Rows.Add(row\[0\].ToString(), row\[1\].ToString() + " " + row\[2\].ToString()); } statusLabel.Text = searchinfo.Rows.Count.ToString() + " row(s) returned";
double clicking or clicking an 'edit' button successfully raises a child form
private void customerList\_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int index = e.RowIndex; DataGridViewRow row = customerList.Rows\[index\]; String cid = row.Cells\[0\].Value.ToString(); editcustomer editdiag = new editcustomer(); editdiag.id = cid; editdiag.ShowDialog(); }
I used a timer to filter results from a textbox that starts and stops based on textchanged
private void search\_TextChanged(object sender, EventArgs e) { if(queryInterval.Enabled == false) queryInterval.Start(); }
here is the 1000ms tick that stops itself after an idle match
public void queryInterval\_Tick(object sender, EventArgs e) { if (lastQuery == search.Text) { queryInterval.Stop(); } else { if (search.Text.Trim() != "" && search.Text.Length >= 3) { customerList.Rows.Clear(); SQLiteParameter\[\] param = { new SQLiteParameter("@search", "%" + search.Text + "%") //used direct string for debug //search.Text) }; DataTable searchinfo = database.query\_search("SELECT id, firstname, lastname FROM customers WHERE firstname LIKE @search OR lastname LIKE @search", param); foreach (DataRow row in searchinfo.Rows) { customerList.Rows.Add(row\[0\].ToString(), row\[1\].ToString() + " " + row\[2\].ToString()); }
The easiest way to do what you want is to create a static Globals class, and create the object in question in that class. At that point, it exists everywhere in the app, and you can read/modify the class' properties from any form. It may not be "the best way", but it's probably the easiest way.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
It's not difficult at all, not really - you've got the full code, and all it's doing is using the same event mechanism that everything else does, just you are creating and raising the event as well as handling it. And creating an event is almost trivial, I do it so often I created a Visual Studio shortcut to to make it even simpler: A Simple Code Snippet to Add an Event[^]. Now I just type "evh" and press TAB and it fills out the event, just like "prop"+TAB creates a property. Raising the event is just a case of calling a method!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
I tried, and got errors, even downloaded the zip and it's pretty confusing. Keep in mind i've been doing c# for like, two weeks? Gotten fairly far using just google searches. Some specific things do elude me. Like your code. Still have a lot of specific stuff to figure out. like things like seeing {get; set;} at top and things like that I just don't understand. Mind you this program is going to be used by just me, nor will I be doing any business-level programming. I did some of that with PHP but never continued and never learned ajax/dhtml, which a lot of web sites require. My specialty is repair/support (which this program is for me to keep track of that) aaaanyways, I found a solution, that I think I did put in my original post, but just using .show allowed my original form call to create a FormClosedEventHandler from the parent, which works. So now the child is doing nothing aware from the parent...sort of
-
I tried, and got errors, even downloaded the zip and it's pretty confusing. Keep in mind i've been doing c# for like, two weeks? Gotten fairly far using just google searches. Some specific things do elude me. Like your code. Still have a lot of specific stuff to figure out. like things like seeing {get; set;} at top and things like that I just don't understand. Mind you this program is going to be used by just me, nor will I be doing any business-level programming. I did some of that with PHP but never continued and never learned ajax/dhtml, which a lot of web sites require. My specialty is repair/support (which this program is for me to keep track of that) aaaanyways, I found a solution, that I think I did put in my original post, but just using .show allowed my original form call to create a FormClosedEventHandler from the parent, which works. So now the child is doing nothing aware from the parent...sort of
Then you are trying to get way ahead of yourself - if you don't understand the basics like automatic properties:
MyType MyProperty { get; set; }
Then you are missing huge chunks, and you need to wonder what else you have missed, and how it might have improved things and made your life - and code - easier. How are you learning? A course, a book, YouTube (gawd forbid!), guesswork?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Then you are trying to get way ahead of yourself - if you don't understand the basics like automatic properties:
MyType MyProperty { get; set; }
Then you are missing huge chunks, and you need to wonder what else you have missed, and how it might have improved things and made your life - and code - easier. How are you learning? A course, a book, YouTube (gawd forbid!), guesswork?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
google searches, guesswork. But as you can tell from my code i'm not doing TOO bad. I know some of the .net concepts, and did pretty well with vb.net (but I hate the syntax of vb.net) did borland C++ in high school and a little in college to get me past the required single beginning programming class (my comp sci was focused for computer repair)
-
google searches, guesswork. But as you can tell from my code i'm not doing TOO bad. I know some of the .net concepts, and did pretty well with vb.net (but I hate the syntax of vb.net) did borland C++ in high school and a little in college to get me past the required single beginning programming class (my comp sci was focused for computer repair)
I guessed it might be that - it shows in your code. Do yourself a favour - a pretty big favour - and get a book (Addison Wesley, Wrox, MS Press all do good ones, and there is also the Yellow Book[^] which is pretty good, and free). A book has structure, and leads you through the topics - and at the very least makes you aware of stuff you might use even if you don't study it well. Random guesses and searching doesn't do that: if you don't know it exists or what it's called you can't find it out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
I guessed it might be that - it shows in your code. Do yourself a favour - a pretty big favour - and get a book (Addison Wesley, Wrox, MS Press all do good ones, and there is also the Yellow Book[^] which is pretty good, and free). A book has structure, and leads you through the topics - and at the very least makes you aware of stuff you might use even if you don't study it well. Random guesses and searching doesn't do that: if you don't know it exists or what it's called you can't find it out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
wow $40. I'm not going to be making any money off my code so and it's a program just for me (and an easy one at that), i'll probably skip this, at least for the time being
-
wow $40. I'm not going to be making any money off my code so and it's a program just for me (and an easy one at that), i'll probably skip this, at least for the time being
Technical books aren't cheap because they take a lot of time and effort to write - and the author has to put a lot of work into learning how to teach and his subject before he even starts. (That's why most "how to code" videos on YouTube are such crap: the author doesn't know his subject well, he does kn ow how to teach, and he has no idea how to make a video.) But the Yellow Book is free (there is a download on the page I linked to) and some of the others can be borrowed from public libraries or bought second hand. For those a lot less ethical, many of them can be stolen from the internet as EPUB or PDF files as well. If we go back to cars, you are trying to learn to drive a F1 race by stealing Ford Fiestas and stamping on the throttle. If you don't know how the clutch works or even that "it's the pedal on the left" your gear changes are going to be noisy and clumsy forever; if you don't know which side the petrol filler cap is you're going to have fun at the fuel station; if you don't know that tires need a specific air pressure you're going to wear them out very quickly. And even if you guess all that eventually you still aren't going to beat Louis Hamilton! With C# and the .NET framework, there is so much "stuff" that can make your life easier - if you use it right - that you won't even begin to guess it's in there! Linq, generics, interfaces, using blocks, try...finally, the Diagnostics class, overloading, databses, transactions, delegates, ... the list goes on a very, very long way. The only way to find them is to learn them - and that means a book, or better a course, honestly. Get the Yellow Book, and read it. It's a pretty good starting point.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Technical books aren't cheap because they take a lot of time and effort to write - and the author has to put a lot of work into learning how to teach and his subject before he even starts. (That's why most "how to code" videos on YouTube are such crap: the author doesn't know his subject well, he does kn ow how to teach, and he has no idea how to make a video.) But the Yellow Book is free (there is a download on the page I linked to) and some of the others can be borrowed from public libraries or bought second hand. For those a lot less ethical, many of them can be stolen from the internet as EPUB or PDF files as well. If we go back to cars, you are trying to learn to drive a F1 race by stealing Ford Fiestas and stamping on the throttle. If you don't know how the clutch works or even that "it's the pedal on the left" your gear changes are going to be noisy and clumsy forever; if you don't know which side the petrol filler cap is you're going to have fun at the fuel station; if you don't know that tires need a specific air pressure you're going to wear them out very quickly. And even if you guess all that eventually you still aren't going to beat Louis Hamilton! With C# and the .NET framework, there is so much "stuff" that can make your life easier - if you use it right - that you won't even begin to guess it's in there! Linq, generics, interfaces, using blocks, try...finally, the Diagnostics class, overloading, databses, transactions, delegates, ... the list goes on a very, very long way. The only way to find them is to learn them - and that means a book, or better a course, honestly. Get the Yellow Book, and read it. It's a pretty good starting point.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
yeah I know about try...finally some generics and heh yeah overloading is beyond me. I've read about it and seen code samples, and delegates I have a blurry idea at best, because I have seen other code samples but yeah the base concept will have to be learned in a basic to advanced way a book does again this program is easy, well...yeah I did have that big problem, but I overcame it. It's simple database and form stuff. I know a good amount about databases already with my time using php/mysql, but yeah I know there is a unifying sql subsystem in c# I haven't played with that has to do with sources, and binding, etc