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!
Related
I am making my C# project and I want to clear the Gridview but the main purpose is after the Gridview is cleared, only a new data inserted should be seen however the old data is saved in my database.I was able to clear my Gridview but after inserting new data my old data is also shown in that gridview which should not be seen.Will be thankful for your suggestions.
I'd say that in most cases the DataGrid is bound to a DataSet. You could simply exchange the DataSet with a different one. Or maybe you modify the contents of the original DataSet, but in this case you would maybe need to cut the connection to the database if you don't want the data getting persisted...
I have a huge table and I need to implement a (web) control to allow me to browse through all records, paginate, filter and sort them. My first thought is to implement a DataGrid/something and to put as DataSource a DataTable which is read (with a "view" or simple sql "select all" statement) and then to filter the DataTable object (DataView.RowFilter, etc). As far as I am aware, this sorting/filtering is done on the client-side, after all records are read. Is there any more elegant/efficient way to do this (meaning - just read the records you need from the database server)?
For controls like GridView the default paging retrieves all rows from the database each time a page is selected.
Not exactly efficient is it? However, you can use custom paging to retrieve only those records you need for the currently selected page.
Here is an example: http://www.asp.net/web-forms/tutorials/data-access/paging-and-sorting/efficiently-paging-through-large-amounts-of-data-cs
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 have inserted the screenshot of my Windows Form which pulls up information from a Product Table:
Now as you can see i have allowed the option to Modify and then Save the modifications in the Database Table. So the issue i am facing is in updating the data in the Database Table. Since the user can update more than 1 row at a time in the GridView, how do i update the Database table?
I can either do a row-wise update, or i can loop through the GridView and update my table and then use a Command Builder.
I want know how do i achieve the solution in both scenarios.
I would loop through the rows and pass the values to a parameter and then update in database. One thing, it is more work that doesn't need to be done. But another thing that helps is you know whatever cell they edit will be committed to the database.
Using SQL Server Database View for populating DataGridView and modifying data in DB
I want to know that how this will be done and if this is not a good solution, then please elaborate if any other good solution is possible.
1) Initially I want to use a Database View for populating different DataGridViews.
2) Multiple joins from multiple tables are required for the data-retrieval for each individual DataGridView.
3) The values from these DataGridViews are then populated in Controls (i.e. Textboxes/Combo-boxes) (For reference please see the images at this link: link text).
4) When the User changes the values in Controls, and clicks Save, the data earlier retrieved from DataGridView (via View) is to be updated in the Database.
NOTE:I am working on C-Sharp Windows Application and using SQL Server 2005.
1) I'm not sure how you can bind a control directly to a View, but you can edit your DataSet (assuming your using the designer under data sources right click on your dataset and click "edit dataset in designer", once there you can right click and add a table adapter which you can populate however you want (For example
SELECT * FROM [YourView]
Alternatively you could just use a stored procedure or type the SQL there to populate the Table Adapter.
Once you have added a table adapter to your dataset you can bind your DataGrid to that table adapter just as you would to a regular table.
I know this is probably now what your looking for but the truth is that I came across your question while looking for a way to do more or less the same thing.
2) You table adapter can be based on many tables using either a SQL statement or a Stored Procedure.
3) Why don't you directly bind those controls using their datasource property?
4) If the controls are themselves bound then you can change the data through the controls and to see the changes in your datagrid you should just need to "refresh" (call it's databind() method) to see the updated data.