Insert/Update values in a gridview - c#

What would be the best way to Insert/Update the values in a Grid view
on clicking a save button placed somewhere.
in the page .
I dont want that to be done like Using Insert Item template and EditItemTemplate.
My thought:
"Loop through each row of Gridview and
connecting to database and
execute the command
and close the connection."
Or is there any way so that we can connect to database only once and perform
Insert/Update operations.
any suggestions would be appreciated..
Thanks in advance.

May these articles help you:
Bulk Edit with GridView
Edit Update Multiple Records/Rows In Gridview With Checkbox ASP.NET
There are many other related articles:
Editable GridView in ASP.NET 2.0
ASP.NET GridView - Add a new record
Insert, Update, Delete with Gridview....simple way

Related

Custom update gridview and ObjectDataSource

I have a gridview displaying data by using an Obejct Data Source which is then connected to a Business Layer.
The data shown/retrieved is from two tables with a join.
Is it possible to have a custom update method which, when the user clicks edit against the row, it will update only the one table out of the two that's required for the data shown (however if it can update the entire record that's fine too)? If yes how do i tie the ODS, along with my BL? Any info would be appreciated as my research so far has not given any examples of this.
To give an example one table is a user and the other table is results. The Results table is connected to the Users table by a UserID (foreign key) but the gridview shows a little more info when both tables are joined.
You can place a linkbutton inside a template field & give it CommandName="MyUpdate". You can then check for the commandName on rowCommand event of gridview & handle your custom update there.
if(e.CommandName =="MyUpdate")
{
// Perform your update
}

How to update Database Table when multiple rows are edited in Gridview

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.

Update a data row inside an asp.net Data Grid View

I think it would be easier for you to understand my problem if I explain the scenario a bit.
What I am doing is developing a system to a doctor using asp.net c#. For the creation of the prescription, I add a check box list for the list of medicines and submit button below it. When the submit button clicked selected check box list items added to the sql server database and show a data grid with the selected medicines in the same page.
In the data grid there are two columns for medicine name and dosage.Medicine name is taken from the database.I need to give the user the ability to enter the dosage through that grid view. I need to use update command because the record is already there in the sql server.But empty value in the dosage column.I tried to use update command in the data source control. But I can't figure out how to give the WHERE clause because I can't specify the database row which the updating record relate to.
I would be glad to hear some help from you. All the comments are welcome.
You need to set a parameter(s), please check this example to understand how it works. I guess, that you've added in the database some column that is unique identifier.. some autoincrement int or guid.

Synchronizing datagridview and database table

I have a datagridview and i have bind it to database.
But when I delete a record , or insert , or edit , the changes don't apply to my table
Is there any solution?
I want to use a little code and do it by wizard and visual
Create a event whenever a record is added, deleted or edited and have that event fire a fresh databind to the datagridview.
Take a look at SQLDataSource Control, and Bind the GridView with SqlDataSource Control. Take a look at the following url http://www.asp.net/data-access/tutorials/inserting-updating-and-deleting-data-with-the-sqldatasource-cs

how to insert row in grid view

i want to insert a new row to grid view while i click new button with out using the sqldatasource. and also how to edit, update, and delete rows from grid view. pls help me
[visual studio 2008
asp.net with c#]
thanks
thiru
What do you mean by "without using the SqlDataSource" ? How do you intend to propagate the inserted data to the data store, then ? You have not specified what other method you are using.
Anyway, the GridView does not inherently support insertion of records, but you can accomplish it by creating a FooterTemplate in which you create the fields for entry of new data. Additionally, provide a column to allow for Insert/Cancel buttons in the FooterTemplate.
Here's a good sample: How to easily insert row in GridView with SqlDataSource
Good gridview tutorial : ASP.NET Quickstart Tutorials
For adding new row you could try putting controls in footer row of the GridView.
Editing, updating and deleting require you to do two things:
implement those operations in the SqlDataSource
enable these operations in GridView
If you want to do it without the SQL datasource, then set DataTable as a GridView's datasource.
Good example on how to populate and add a row to datatable.

Categories

Resources