Clearing the Gridview in c# - c#

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

Related

Saving data from datagridview to a dataset

I'm very new to C# and visual studio (I have only used python up until this point)
The issue that I have is I want to quickly populate a datatable in my dataset with test data. The method so far was to create a data grid view of said table (PATIENT) fill in the info and either exit the program or press a button to save. exiting the program doesn't save for me and I am not sure what code will allow me to save from a data grid view.
TL:DR Anyone know of the correct code to save a datagridview to a dataset.
Its completely possible I'm not even using a dataset correctly -_-
A DataSet is 'an in-memory cache of data retrieved from a data source' (https://learn.microsoft.com/en-us/dotnet/api/system.data.dataset?view=net-6.0). In your case, it sounds like you haven't bound to an external 'data source'; the grid is empty and the user types in the information. You will need to persist that data out to some sort of storage, most likely a database, if you want it to persist between sessions.

Multiple row input in asp.net for editing database table

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/

Adding rows programatically to a DataGridView that is data bound?

I have a problem and I can't seem to be able to solve it. I thought someone here will be able to help out.
I have a form that has a DataGridView of customers. Now, I want to add several customers to this DataGridView without actually adding them to the database. This is because the client must be able to create a list of clients and when thats done add them all at once.
I have tried this:
string[] array = {"Microsoft", "Redmond", "Something"}
dataGridView.Rows.Add (array);
Now this can't be done because I get an exception saying something in the lines of you cant add rows programatically to a DataGridView that is data bound.
Now I have also read that this can be solved by using a table adapter to insert the rows instead of directly adding them via. a DGV. But this is also not possible because I use custom headers in the DGV because existing data is fetched via JOIN's so if I add them via a TableAdapter I get an exception that the it doesn't match the databases table schema.
Now I am really lost... Anyone know of a (halfway) elegant solution to this problem?
Thank you
Bind your DGV to a BindingList<YourObject>, where YourObject can be a simple class with properties reflecting your database schema. Initially populate the BindingList from the database, and the DGV will automatically add YourObject instances to the list. When you're ready to commit the changes, do so manually from the BindingList.
In order to show those rows, just do a UNION (-like) action before the databinding. Ie get your list of records, add the special ones and then databind.
Updating will require some special action(s) too. But with a total lack of details about your datastructures it's impossible to say what.

What are the possibilities of displaying items in a listbox?

I've been trying to figure out for a long time now how to create an interface that can allows users to input several rows of data and pass those entries into an SQL server database all in one shot. I could not get any better ideas so I came up with this (see picture below).
What I envisioned is that the user enters values in the textboxes and hits the "add to list" button. The values are then populated in the list box below with the heading "exhibits lists" and when the add exhibit button is pressed, all values from the list box are passed into the database.
Well, I'm left wondering again if it would be possible to tie these values from the textboxes to the list box and whether I'd be able to pass them into the database.
If it were possible then I'd please love to know how to go about it otherwise I'd be glad if you could recommend a better way for me to handle the situation otherwise I'd have to resolve to data entry one at a time.
I believe there is some useful information from this website that can help solve my problem but I just can't make heads or tails of the article... it seems like I'm almost there and it skids off. Can everyone please read and help me adapt it to my situation? Post below:
http://www.codeproject.com/KB/aspnet/ExtendedGridView.aspx
Yes, it is possible. I have done this twice before.
Check out the "Data Access Tutorials" at http://www.asp.net/web-forms/data for ideas.
Overview
For the user-interface, you want a data-entry-form above a data-grid.
For the back-end, you want a data-adapter (or table-adapter) that loads and saves data to the database in one operation.
You would use a DataSet and DataAdapter to update the database, and manipulate a DataRow for the input and a DataTable for the list-box. Note the list-box is actually a data-grid, repeater, or other control that accepts a DataTable as a DataSource.
The DataAdapter fills the DataSet which contains the DataTable.
The input controls bind to a DataRow created from the DataTable. When the user clicks add-to-list, you add the data-row to the data-table, then create a new data-row for the next item.
When the user clicks add-exhibit, it is a simple matter of using the DataAdapter to update the database. Any changed, deleted, and added data is handled for you.
You could use a transaction to encapsulate all the sql queries you are trying to run, so you can then just 'commit' the transaction in one to the database? Are you still having difficulty with this issue?

C# datatable, dictionary, datagridview

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!

Categories

Resources