Populating C# combobox from SQL and restrict results based on choice - c#

I am binding some data from a SQL table into a C# ComboBox. What I want to achieve is the following:
Say we have 3 tables in SQL
Room (Bedroom, Bathroom, Hall)
Stuff (Bed, Bath, Wardrobe)
StuffProperty (DoubleBed, HotRunningWater, Clothes).
My goal is to restrict the second and the third ComboBox values, based on the choice of the first ComboBox, i.e:
The first ComboBox contains Bedroom, Bathroom, Hall. If the user chooses Bathroom, the second ComboBox contains only Bath and not Bed or Wardrobe and the third ComboBox only contains HotRunningWater, not DoubleBed or Clothes
How can I send a query back to SQL without pushing a button, 'live'?
Also, should I use Stored procedures to map Room, Stuff and StuffProperty values together and invoke them in C# code (this is what I would prefer)? Or should I write the whole filtering stuff in C# (for example using switches)?
Really clueless on this one and not even sure if there's any way to 'inform' the SQL of a recent choice without pushing a button. The columns I used are only to illustrate the problem.
Also, I'm aware this question has been asked here a couple of times, but all I found were VB code and I need it to be sorted with a Stored Procedure and C#, or C# only.

You would need to hook onto an event of the combobox, most likely selectedIndexChanged or similar. This is how you get the "live" effect. You would need to hook onto the event of the first combobox and then populate the second.
Then in the event of the second you need to populate the third.
How you go about this using metro wizard I am not to sure but hopefully with a gameplan you can find the required code.

Related

c # winform mysql data display

through C # I am creating a program that manages ambulances, I am trying to create a list with the events and active missions, this data must be taken from a mysql database, I have tried to see many "tutorials", but none explained what I needed, how do you think I can do something like this?
image here
in addition to displaying the data, if I click twice it should open the event or mission to me depending on where I click, (the icons are also buttons that should only be visible if there is a data)
just a tip! I'm not asking you to write the code for me!!!
EDIT:
I managed to get the data from mysql, that you know is it possible to place the data in specific columns?
Program
Code
It is difficult to guide you through such unspecific question, but I'll try some hints.
You said you have data from MySQL
Fill a DataTable (another way is to use BindingSource)
Assign DataTable as a DataSource of your DataGridView grid.
You can edit columns in the DataGridView using UI in VS, or you can do it programatically. First is easier, just note that if you at any stage provide Nothing as a data source, you'll loose column headers.
For event fired on DataGridViewRow, click on the grid in Designer, click on events in the Properties pane, find DatagridviewRow_DoubleClick and double click the line, to create associated method
FIll in the code into the even method. Use DataGridView.SelectedRows(0) to get selected row and it's index. If you want to "open it" create a modal dialog or what you want for displaying the content.

Inserting XAML elements through C#

I'm just starting with Universal Windows Platform and actually C# as well (I've got a C background) and just trying to get my feet wet with a basic accounting app meant to keep track of a checkbook. The way it works is simple, the user either presses + or - buttons and specifies the name of the transaction and the amount either credited or debited, hits ok and that transaction is inserted as a row.
So it would look something like this
Gas ($20) 1/5/17 _delete_button_
Paycheck $2400 1/3/17 _delete_button_
Total $2380
....
So each row has 2 editable text boxes (name and amount), one textfield for date, and one button to delete that row.
Now my first instinct as a C programmer is just to create a struct with those variables and every time user inserts a row just populate the struct and push it on a stack. However I just want to make sure that this is the best way to do this and I'm not missing some feature of XAML that would let me do this.
"Now my first instinct as a C programmer is just to create a struct
with those variables and every time user inserts a row just populate
the struct and push it on a stack. However I just want to make sure
that this is the best way to do this and I'm not missing some feature
of XAML that would let me do this."
This question is dangerously close to not meeting the requirements for a good question because it is too broad, but I will answer anyway due to remembering the pain I went through with XAML when new. Whether new or experienced (and I am at most in the "intermediate" category), it seems XAML has its bits that will always send you looking for help. But I digress.
The standard is to create your model, then somehow connect (or "bind") the appropriate parts of the model to your view.
You would use C# (actually can use VB or F# or others also) for your model. This is where you would create your "struct" you are referring to. This would define your individual transaction. Then, create a collection of these transactions, again in your model.
In XAML, you bind to this collection. You have a control (a visual element in your view) that displays all transactions. You have a control for specifying the name of a specific transaction, for selecting a transaction, for inserting, for deleting, etc. All of these controls interact with the model through the bindings.
So, to answer your question, yes you are missing a feature of XAML. It is called binding. Not to be sarcastic but unfortunately, binding is a technology worthy of months of study. Sorry, but you will want to go back to C with no UI at first. Keep at it! If you want to do the universal app thing, it is worth it. You may even find yourself liking (parts of) it.
Link to get thou started:
WPF and other XAML-related

Call code behind c# function from javascript

I know this has been asked, but none of the numerous answers fit with my situation. So I will humbly ask if someone can walk me through this again gently.
Environment: .Net 4 web app using c#, javascript, jquery, sql server, and entity framework.
Question: I need to be able to allow a user to "quick add" a value into a table that is bound to a drop down box without losing values a user has already entered onto current page. I don't care if I have to do a postback or use jquery or use ajax, etc. I need the functionality first - prettiness later ... the scenario is described below if you care to keep reading. With all that being said, I would love for this to be smooth and not "clunky" for the user.
Scenario:
I have a page where the user is able to enter many pieces of information into textboxes, static drop down boxes, and drop down boxes that are bound to other tables that will all get combined and inserted into one table. Well, it never fails, a user could be filling in the page and when they get to one of the table bound drop down boxes - a value is missing or they haven't added something item yet. I would like to add a button or link or something next to that drop down box that would allow the user to "quick add" an item to the table that fills that drop down box and then refresh the drop down box so they can choose that new value from the list ... all while not losing the other data on the screen they already entered.
What I've tried:
-Tried WebMethod option. Currently, this web page calls some custom "bind" methods in the code behind to fill drop down boxes so they are not filled directly from objects from entity framework. So that means using a static method will not work for a solution because part of the static function would need to call the bind method to refresh the drop down box with the newly inserted values.
-Tried PageMethod option. Some of the other samples used Page Method settings, but my site uses master and content pages and the posts seem to state that the page method route was not going to work.
Make sense?
Add an UpdatePanel to the page. The update panel should wrap the drop down to be added, as well as the textbox/button for adding a new entry to that drop down. You would want to have a different UpdatePanel for each dropdown/button pair, if applicable.
When they click the button inside of the update panel you can read the textbox, add an extra item to the drop down, and send an update to the database. (I'd manually add the new value to the drop down rather than updating the database and re-binding, if possible, but it may not be.)
The magic of UpdatePanels will make sure that this is all asynchronous and so doesn't disturb the user working on the page.

How to approach data-binding a BindingList of Lists to a DataGridView in C#?

I'm writing a program that functions as an Excel-style dictionary. Basically, it allows the user to add rows, edit rows, search through them, and so on. I use it for storing and studying vocabulary for foreign languages.
I've gotten a version up and running that I'm quite happy with. It uses a BindingList as a data source for a DataGridView in order to track changes and record them back to the BindingList that I use to store all the vocabulary, and the list itself is made up of a custom class I named "Term", that has properties for "English Word", "Spanish Word", "Examples", ect. What it doesn't do is let the user customize the fields, and that's where my problem comes in. It's pretty much "hard-coded" in that even if I'm studying Spanish or French, the Term class is going to be using the property for "Kanji" from Japanese.
I want to be able to have the user type in what fields they want the dictionary to display and keep track of--basically, they should be able to rename and add/remove columns from the DataGridView. My first thought was to implement this as a List, which stores the names of the fields (and accordingly the number of them, by using the List's length). Then, I would have a Word class that has a List property, and each string in the list represents one of the fields. Then I create a BindingList of this Word class, which leaves me with a BindingList of Lists.
When I try to databind my List list to my DataGridView, the grid comes up empty--it apparently has no idea how I want the data to be displayed and I'm having great difficulty figuring out how to tell it to. I'm not even sure if my approach of having a List of Lists is a good way to implement customizable fields, but it's the best I could think of. In any event, can anyone recommend a way to approach this that lets me add the fields to the table, but also tracks changes and pastes them back to the original source? I need the grid to be used as an editing tool for the user to not only add new elements, but also change existing ones.
It's a personal project, but it's driving me a bit crazy. I was up until 5AM last night trying to figure it out and came up empty-handed. Thanks very much for reading!
I've read your post a couple of times. I'm not sure I understand completly. If I don't, please give some details and I'll try to help.
If I had to do a Excel-like DataGridView, I think I'd use an Array. I would create an array of, say, 256 by 256 and put it as DataSource. Then after the user edits, you read the whole DataGrid and rewrite if it differs from the array you originally had.
I think you might be interested in this class:
http://www.codeproject.com/KB/grid/DGVColumnSelector.aspx
It allows the user to dynamically display which columns are shown in the DataGridView

Storing a collection into a list and session - Front End

I have this program in which I am trying to store a collection of values into a list and session
Example:
valueCollection = (List<Values>)Session["Value"];
I want to do the front end in a way which will show a table with each row showing a label and textbox. This would be too simple to do obviously but I want it to show 4 rows of the table by default and then the user can select "add another" this will then add another row onto the table with a label and textbox exactly similar to the 4 default. Everytime the user selects "add another" the table increments by 1.
How do you do something like that, or is there an easy way?
Here is a screenshot to explain it better:
http://img828.imageshack.us/img828/9986/idead.png
The basic idea is that you bind a control to your data and provide GUI controls to allow users to add/edit/delete records. Once the records are modified you re-bind the control to see the changes.
Take a look at the Databound controls in the .Net Framework. For a quick first pass you could use a ListView - a control which provides built-in functionality to automatically generate and mange the add/edit/delete GUI elements.
In 'real world' usage, storing big datasets isn't a great idea as more users = more sessions and each session uses server resources. You should really be getting/storing your data in a database or perhaps an XML file.
HTH

Categories