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.)
Related
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.
From time to time, I need to create an input control which allows multiple rows of input, the type of input and number of columns could be different in each case but typically it would be represent a database table being edited, so it should also be possible to store an id value within each row. It should also be possible for the user to add new rows to the input if they have more data to input.
I have tried a couple of approaches to this but they are all very long winded for what seems like such an obvious and common scenario. I'm thinking that there must be a simple way to do this that I have missed.
The way that I usually solve this is by using a list view, enter all of the input controls required within the item template and use a html table for formatting, with each item being a row of the table. If there is existing data to be edited, I would load the data from the database, add a blank object to the results and bind it to the list view. If there is no existing data, I would create a collection with a blank record in it and bind it to the list view. I add a button for adding a new row. When clicked, this button retrieves all of the existing data from the list view by iterating all of the listview items and populating the data into a collection, which I would then add a blank object to and rebind the listview with the results. When saving, I would retrieve the results by iterating the listview items again and using a hidden field within each row to store the record id. This would determine whether a new record was added or an existing record was edited.
This approach works for me but I'm sure there must be simpler ways to achieve this. Looking forward to seeing how other people solve this.
For web forms, use a GridView. You can set its properties to allow editing, deleting, new rows, sorting, and paging.
Check out the MSDN example here that shows how to use it. If you bind a datasource to it, it will figure out the columns and adjust dynamically then, or you can predefine the columns you want for more customability.
You are talking about bulk insert/update. You could use XML insertion/updation for this purpose. Get all the data to a DataSet ds variable. Then use ds.GetXml() method to convert the dataset to XML string. Pass this to an XML parameter into SQL server which has the datatype 'XML'
INSERT INTO YOURTABLE
SELECT
V.VOI.value('(.)[1]', 'int')
FROM
#Input.nodes('/DATASETNAME/DATATABLENAME/') V(VOI)
Use this link to learn more
You need dynamic table with Add, View,Edit and delete operations on each data.
I would suggest using DataTable Jquery component. there are tons of examples on Data operations, and you can plug this with any Server technology including ASP.net
http://editor.datatables.net/
I'm trying to remove selected item from ComboBox Collection:
Items are added manually, as a Collection, in design time.
buttonClick:
cb01.Items.Remove(cb01.SelectedItem);.
This deletes the item, but next time I open the Form - the item appears again.
Must I have a database for 5-6 items ?
Please help.
This cb01.Items.Remove(cb01.SelectedItem); will only remove from the combobox, not from the datasource binded to the combobox. You may remove it from the datasource and re bind the source.
If you are binding the combobox with an array in your code, then you may save the array on a persistent storage, either a database table, or XML file and upon deletion from combobox you should remove the element from the array and save the changes to the persistent storage
You can also work with Files (existing in System.IO Namespace) if you don't want to use a database server. for 5/6 items it's not worth to use database, and in a file you can easily find the item's line and delete the line.
hope it helps.
I have a project where I want to:
1) grab data from a sql server database,
2) pull the data into a c# program,
3) keep the data persistent - (store in a dictionary?)
4)view the data using a datagridview,
5) update the data, which would update the datagridview, the dictionary, and the database
What would be the most efficient way to implement this project?
My current thinking is to use a datatable to keep data persistent in program, and to allow data to be easily viewable. As well.
Any thoughts?
You can bind a DataGridView directly to a datasource (SQL Server) as described here
Create DBML, and use LinQ.
Get your data, and bind them to custom DataTable (which is created with your own column names, types etc.)
Bind your DataTable to gridView.
Put a Update button, and when user selects a row, clicks an update button, get selected row, Update this row with LinQ and refresh your gridView.
phsr's answer takes care of the UI and database. In order to store it locally you could use a SQL Express database, or an easy alternatively would be to simply store it in local XML files, see here for some help with that:
http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx
Hope that helps!
I have a DataGridView bound to a LINQ to SQL query expression. I want it to be editable for updates, but for inserts I want users to use separate controls below the grid (textboxes, date pickers, etc - currently not bound to anything). I have an "Add" button that creates an object of the linq to sql entity class based on the values in the controls, and then calls InsertOnSubmit(). When I later call SubmitChanges(), any updates from the grid, and any objects added are all correctly persisted to the database.
My problem is that any new objects are not added to the grid, before or after the call to SubmitChanges(). I would like new objects to be added to the grid as soon as they are created. Also, I only want the controls below the grid to be used for inserting new records, not editing existing records, so I don't think they should be bound to the data source...What is the best way to make this happen?
I did try simply re-setting the DataSource for the grid (ie dataGridView.DataSource = db.<TableName>, which works, but is clumsy because it scrolls to the top of the grid again - I'm sure a better method exists.
(Pls excuse the n00b question, I'm very new to all this .net stuff :P)
The first thing to try is GetNewBindingList(), but this thread: "Linq-SQL canonical editable datagridview sample code" has some thoughts for other scenarios.