Synchronizing datagridview and database table - c#

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

Related

How to edit the GridView and Update values if the GridView is user defined

I have a GridView which is completely user defined. The first column of the GridView is from the ListBox1. The headers are defined using ListBox2.
There fore I need to update rows of the GridView with some integer value and make sure the Gridview holds that value until a button is clicked.
Once the button is clicked I want to read each columns of the GridView that has values entered and create a table in the database with the headers names as columns and just replicate the GridView as table in database.
I have attached a screenshot of my GridView. Kindly help me fix this problem.
There are 2 key things to implement UPDATE logic in a GridView
In the GridView markup, you have to add an eventhandler for OnRowUpdating event
<asp:GridView ID="XXX" runat="Server" .....
OnRowUpdating="UpdateRecord" ..........>
Then you need to implement this "UpdateRecord" method referred above with your custom code which will basically retrieve the data from the grid row and update it to your datasource
You can refer to some sample code of how this can be achieved here
To point out again, in your case, since you want to dynamically create the table as well, you will need to additionally do that as well. Personally, i have no clue why you would go with such a design but in absence of any information, i assume you know best.

Insert/Update values in a gridview

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

C#.Net : Edit and Update the dynamically bound grid view

In my website I am dynamically binding the datasource to the grid based on the value in dropdownlist.
I want to edit and update the values in grid view and the respective updations should be done in the original database.
How can I do that?
In the most basic and direct way, you use the OnUpdateCommand event of the datagrid to invoke a server site handler. That handler will receive a DataGridCommandEventArgs parameter containing an Item property which is a grid row with updated values. Retrieve key and new values from that row and build a corresponding update command.
do you know how to bind dropdownlist vai datasourase?
and witch data sourse you use tell me first.
else you just make a SELECT query and fill the dataset after that you you must have to bind dropdownlist like this....
ds = dropdownlist.DataBind();
i think this help you.......
else you can tell me if any problem occure in this code..........

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.

edit datagrid row

i want to know how to edit a single row (which i select) from a data grid
for example i have a datagrid with columns A, B and C
and i have a couple rows of data, approx 10 rows.
lets say i want to change the value of data within row 4.
how would i do this?
i am using visual studio 2003, but i guess if visual studio 2005 would be okay too. for the coding I'm using c#
thanks..
All grid-like components of asp.net have the same machanism as it comes to starting to edit a single row. Actually it's default for asp.net only to edit a single row in a grid.
Needed to start editing is to include asp:button or asp:linkbutton in the ItemTemplate with the CommandName set to "Edit". This one of reserved commandnames all grid-like components knows how to respond to. Clicking this button in DataGrid will raise the EditCommand Event. In this event you have to set the EditItemIndex of the grid equal to Item.Itemindex of the eventargs. This will render the row vaccordeing to the EditItemTemplate.
In this template you put 2 buttons or linkbuttons. One should have the CommandName set to "Update" and one should have the CommandName set to "Cancel".
The "Update" button raises the UpdateCommand event. In which you execute code that store the data in the row to its storage (eg.: database) and sets the EditItemIndex to -1 --> all rows are rendered read-only(ItemTemplate or AlternateItemTemplate).
The "Cancel" button raises the CancelCommand event. In the event handler you have to do si set the EditItemIndex to -1.
This description is only true for DataGrid en not for the in asp.net introduced GridView which handles most of this "Boilerplate"code it self working together with the datasource controls. Google the web for more info on this. it's to much to explain here right now.
Hope it helps?
Take a look at the documentation for adding an EditItemTemplate to your datagrid. You use the ItemTemplate for view-only, display elements and you use the EditItemTemplate for controls used to bind against a single row that you select.
Here's a link that might help:
http://www.gridviewguy.com/
Is your data in a DataTable before making it a DataGrid, or can you put it in a DataTable? You can update/delete/edit rows in a DataTable, here's a link with code snippets, pretty straight forward:
http://msdn.microsoft.com/en-us/library/tat996zc(VS.80).aspx

Categories

Resources