WF bind data from foreign table to combobox - c#

I'm new to data driven MS programs and I'm trying to build application with WF and EF, but it have problems with data binding, and here is an example.
Here we have some model :
And by DetailsView I added PhoneNumberSet to form, and I binded PersonSet to Combobox to display list instead of foreign key.
But I don't know what Selected Value field supposed to be (I tried every logic option, but result is always the same)
After compiling this app I can add new records, and fill Number field, but when I select person from dropdown, controls stop to work. I can't switch to different record, save current, add new. Dropdown keeps focused, and only way to close program is by stop debugging.

Related

Reorder Listbox and save new order to SQLiteDatabase

I was wondering if somebody knows the right approach to save the position of reordered items in a listbox to a SQLitedatabase so when the wpf app opens next time it shows the saved order.
Where do I save the order and how does it update the database when I change the position of the items in the listbox again.
1- define a column in table called sequence or any suitable name you like
2- when you fetch data from database, always order by this field
3- when you change in app, you have two options
- you change the order on every change event that can be too many calls to database
- once you are done with ordering, you can have a save button or on leave event of the screen and send the data to database with the order and just set the sequence in database.
This way next time you get it, it will be saved sequence.

Best way to display data in a listbox from DB in C#

I have a local database, which holds records for cars, it has fields ID(ai, primary), plateNumber, Comments, ect..
Also I have a form with ListBox, button "Checked", and many text fields.
I am using winforms, and every time I run the application, constructor retrieves all the records from the db which have the bool field named "checked" == null, and displays the carnumbers in the Listview. When a specific plateNumber in that listBox is clicked, the other column data for the selected carPlate should appear in the textfields, but the
Problem is that there might be more records with the same plateNumber where "Checked" == null(true otherwise).
Question: how to keep the information about the specific plate number "behind the scenes" and when person clicks to select the platenumber from the listBox, it displays the data about the current(by id) platenumber in the form. Note that, if person clicks a "Checked" button on the form, it stores "TRUE" in the DB "checked" field and removes the specific carPlate from the listBox, and the next time application is launched it won't display that specific ID. In web I use hidden fields with javascript to achieve this, in C# I'm not aware how you keep information like that, I'm leaning towards arrays?
Thanks.
ListViews are populated with ListViewItems. The ListViewItem class has a Tag property, where you can store anything you want. Your "hidden" or "behind the scenes" data can be stored there.

How to populate and select data in comboboxcell in datagridview

Friends, I've a datagridview in my windows application. It has 6 columns, out of which the 2nd(colindex 1) is of type combobox. Others are of textbox type. I've to populate this combobox from table1. There are two columns in table1 - ID and Name. Only Name will be displayed in the combobox. User can select any one from that combobox and write something in other 5 columns. Upon pressing save button, the ID of the selectedItem from combobox and other 5 textboxes' value will be stored in table2. When again the form will be loaded it'll fetch data from table2 and display corresponding data in datagridview. I've used datasource to populate the combobox. But it's not working. Do any one of you have any idea, how can I do it?
Try Setting the DisplayMember of the combobox to "Name" and the ValueMember of the combobox to ID.
And Also the bindings (For that matter any changes) you perform at design time is in InitializeComponent method of your class, depending on the version of Visual Studio you're using, It'll reside in a same or separate file.

Parent DataGridView does not update when using TableAdapterManager's Hierarchical Update

Here's are the facts:
I have a parent table, let's call it Order. The data in this table is viewed using a DataGridView (dgvOrder).
I have a child table, let's call it OrderDetails. The data in this table is viewed using a DataGridView (dgvOrderDetails)
I have textboxes (and checkboxes, comboboxes, etc.) that are databound to the Order table (using BindingSource).
I am using Visual Studio 2008.
What I want to do:
dgvOrder is read-only. I use this to browse through records ala BindingNavigator. (I like to use a DataGridView for navigating records because it is intuitive. You immediately see what the next few records are all about.)
To add new records, I use a "new" button (uxNewButton). To save my updates, I use a "save" button (uxSaveButton). Records are added using the textboxes and not directly via dgvOrder. Child records are added directly via dgvOrderDetails.
dgvOrderDetails, I guess, is pretty obvious - it contains the details of the Order (1 to many relationship).
My code and configuration:
Hierchical Update (in the dataset designer) is set to true (by default in VS 2008)
Relationship between Order and OrderDetail is set as "both relation and fk constraint". Update and delete are set to cascade. Accept/Reject rule is set to "none". Nested Relation is unchecked.
TableAdapterManager is dragged to the designer(tam).
private void uxNewButton_Click(object sender, EventArgs e) {
bsOrder.AddNew();
}
private void uxSaveButton_Click(object sender, EventArgs e) {
this.Validate();
bsOrder.EndEdit();
bsOrderDetails.EndEdit();
tam.UpdateAll(dsOrder); //DataSet is named dsOrder. try-catch block excluded for simplicity
}
Problem:
After filling up all the textboxes (and etc.) and the dgvOrderDetails, hitting the save button will cause an error: ForeignKeyConsraint FK_Orderchild_Order requires the child key values (-1) to exist in the parent table.
However, if I add a record directly using dgvOrder (then dgvOrderDetails), hitting the save button will save successfully.
I also found out that I can save successfully if I add new records using the textboxes but have to select a different row in dgvOrder, then selecting the current row again, before adding the records on dgvOrderDetails.
It seems like while adding a new record via the textbox, the underlying data is not synced with the DataGridView. Selecting a different row, then selecting back the current row syncs the data altogether.
I have tried various hacks in the save event like:
dgvOrder.Refresh(); //or
dgvOrder.Invalidate();
dgvOrder.Refresh(); // or
dgvOrder.Parent = null;
Controls.Add(dgvOrder);
//and so on and so forth (suggetstions from Google searches)
Adding bsOrder.EndEdit() on the Enter Event of dgvOrderDetail seems to solve the problem but I'm not sure if this is best practice.

C# Windows Application Form and Data base

I am loading elements (a row) from a database in a list. Which technique should I use to update the list when a new entry is added in that table ( SQL database) so that the lists updates and if I have selected an element from the list it won`t get deselected (like Outlook when new mails are received and you have a mail selected ).
If your dealing with simple database transactions, I would recommend a foray into the BindingSource Component. The BindingSource component can take care of creating, reading, updating and deleting things so you don't have to.
I assume your rows have a unique identifier. You can store this Id before you add the new row, and after it's added you can search the list with the stored Id to re-select that row.
(Actually it would be wiser to store the row handle and re-selecting it directly with the handle. But it wouldn't work if your grid creates new row objects to display the updated list. Since I don't know how you bind your data, I cannot say this would work for sure.)

Categories

Resources