Reorder Listbox and save new order to SQLiteDatabase - c#

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.

Related

How to save my datagridview with c# winforms bounded to a datatable

I have bound my MSSQL datatable to a datagridview using the designer (the datatable has atleast 30 columns), it fills the data like this:
this.produktaiTableAdapter.Fill(this.veiklosDuomenysDataSet.Produktai);
this.produktaiBindingSource.Filter = advancedDataGridView1.Columns[1].HeaderText + " = " + Veiklos_ID;
It is necessary for me to bind it like this, as I have a lot of code concerning every single column in the DGV (for example, width, currencies, and so on).
Now I want to save all the new, updated, and deleted data in datatagridview to my MSSQL datatable by clicking a Save button. How can I do that? How can I convert my edited datagridview to the datatable?
I also tried this code on my save button click:
this.Validate();
this.produktaiBindingSource.EndEdit();
this.produktaiTableAdapter.Update(this.veiklosDuomenysDataSet.Produktai);
And.. It doesn't work. I am fairly new with datagridview and datatables overall as this problem I think should have an easy fix.
EDIT:
Also, could this line help somehow?
DataTable Produktai = advancedDataGridView1.DataSource as DataTable;
EDIT2:
This doesn't work either.
veiklosDuomenysDataSet.Naudotojai.AcceptChanges();
this.Validate();
this.produktaiBindingSource.EndEdit();
this.produktaiTableAdapter.Update(this.veiklosDuomenysDataSet.Produktai);
MessageBox.Show("Data is saved!");
EDIT3:
This is a debug picture of my main form: Here
As you can see I have no textbox or buttons like INSERT, DELETE and UPDATE. The user can edit the DGV simply by clicking on a cell and editing the value, add new rows simply by pressing ENTER, deleting the row by clicking Delete (Ištrinti in picture) on the same selected row (NOTE: delete button column is the only unbound column in DGV).
I use MSSQL datatable to bound it with the datagridview, and I want all the rows that are new, deleted or edited in the datagridview to be renewed/updated in the datatable that it is bounded too by save button.
Maybe there should be something different with this information, a wild guess?
Start over - this will generate a basis for comparison
I didn't encounter any problems saving when I did:
Make a new project
Open server explorer, add a connection
Choose SQLServer file driver, put a path to a new file (new.mdf), click OK, say yes to create it
Expand the new node in server explorer, right click tables, add a new table with column names that are sensible (just letters and numbers, no spaces, no punctuation or other symbols - you could add percent signs etc and it'll still work, but there's a reason why no-one does it, and that's because it makes life very hard work all down the line. If you think want a column called "Profit %" call it "ProfitPercent" instead). Save the table.
Open data sources tool panel, add a new datasource, choose database, choose dataset, choose the connectionstring for new.mdf
Really carefully read the next question it asks. Do not just skip past it. If you say Yes then, by default your program will dynamically attach a database that gets erased and replaced every time you run the program (but this is typically what you want). If you say No, then understand that the db your program edits might not be in the project folder at all
Tixk the table in the wizard, or close the wizard and drag the new table out of server explorer into the dataset. Save the dataset
Open the form, drag the table representation out of the data sources tool panel and onto the Form. A datagridview appears
Run the program, add some rows, click the save button
Stop the program, run it again - the rows will be there if you said No to the important question earlier. If you said Yes, then stop the program, click the new.mdf file in the Solution Explorer, change "Copy Always" to "Copy if Newer", run the program again. Add rows, save, stop the program and run it again, the rows will be there. The only time they will disappear is if you change the design of the new.mdf database (so that it is newer)
Your form code will now just be something like:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myTableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.myTableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.newDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'newDataSet.MyTable' table. You can move, or remove it, as needed.
this.myTableTableAdapter.Fill(this.newDataSet.MyTable);
}
}
And you won't have written any of it. It's all that's required to load, show, edit and save data using a datagridview. The call to tableAdapterManager.UpdateAll is equivalent to someTableAdapter.Update - a tableadaptermanager is a convenience device that calls Update in the correct order to ensure that parent rows are created before related child rows
There's nothing wrong with the posted code in terms of "that's how you save" so the diagnosis needs to methodically chase things through:
Run the program, load data into the grid, edit some of it
Pause in the debugger just before you update, and call GetChanges on the datatable that is about to be saved
If there are no changes, then that table was not edited (the grid is bound to a different table) or no edits were made (the grid edit was cancelled not committed) or the edit's were reverted or accepted (RejectChanges or AcceptChanges was called on the datatable)
if there are changes move to capturing the return value from tableadapter update (Write var x = tableadapter.update(..)) and step the debugger over the call to update
if the result is 0 then no rows were updated; this should result in a concurrencyexception if there were changes and the tableadapter query is enabled for concurrency (probably the default). If your tableadapter doesn't have an update query built in then again you should get an exception if you try to save edits ("update requires a valid updateconmand when passed a collection with modified rows". If it's just 0 and no exception is thrown then for some reason no rows were updated - not a situation I'd expect and would need some more investigation
if the result is > 0 then an update was made (changes were saved). On a tableadapter the Update method is responsible for all inserts, updates and deletes. If you can't see your changes it you're most likely looking in the wrong database file, or your build process is erasing your file and replacing it with a new one. This most often happens with file based databases like Access but can happen with sql server if your database file is being dynamically attached and detached

how to refresh my DataGridVIew without stopping the code execution

I have a table BOOK that contains (id, name, price... ) I want to edit the price of a book and i want the new price to be shown on a data grid view automatically while the code is running how can i manage to do it using threads, i m not having problems fetching data from data base , i want the DataGridView to be refreshed automatically once i change the price of a book in SQL server and i want this to happen without stopping the code and run it again or with a refresh button i want to accomplish this using threads.
Easiest solution would be to use a timer (https://learn.microsoft.com/de-de/dotnet/api/system.timers.timer?view=netframework-4.8) that refreshes your datagridview.datasource.
Be aware that this will replace all rows. If you want the user not to see that you could call .SuspendLayout(), do the refresh and then call .ResumeLayout.
The better solution would:
- use a BindingList as the datasource of the DataGridView
- use a timer that gets only the updated objects from the database and replaces them in the BindingList
- due to the BindingList the DataGridView will automatically know what objects have changed and will only redraw those lines

WF bind data from foreign table to combobox

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.

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.)

Cleanest way to implement collapsable entries in a table generated via asp:Repeater?

Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of a very large system, so it really wouldn't be wise for me to revise the table structure (the app is filled with similar tables).
This is a webapp in C# .NET - data comes in from a webservice and is displayed onscreen in a table. The table's rows are generated with asp:Repeaters, so that the rows alternate colers nicely. The table previously held one item of data per row. Now, essentially, the table has sub-headers... The first row is the date, the second row shows a line of data, and all the next rows are data rows until data of a new date comes in, in which case there will be another sub-header row.
At first I thought I could cheat a little and do this pretty easily to keep the current repeater structure- I just need to feed some cells the empty string so that no data appears in them. Now, however, we're considering one of those +/- collapsers next to each date, so that they can collapse all the data. My mind immediately went to hiding rows when a button is pressed... but I don't know how to hide rows from the code behind unless the row has a unique id, and I'm not sure if you can do that with repeaters.
I hope I've expressed the problem well. I'm sure I'll find a way TBH but I just saw this site on slashdot and thought I'd give it a whirl :)
When you build the row in the databinding event, you can add in a unique identifier using say the id of the data field or something else that you use to make it unique.
Then you could use a client side method to expand collapse if you want to fill it with data in the beginning, toggling the style.display setting in Javascript for the table row element.
just wrap the contents of the item template in an asp:Panel, then you have you have a unique id. Then throw in some jquery for some spice ;)
edit: just noticed that you are using a table. put the id on the row. then toggle it.

Categories

Resources