Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit Datagrid without DB Update

Status
Not open for further replies.

fdsouth

Technical User
Jun 6, 2001
61
US
I have created a ASP.NET datagrid (c#) that is bound to a SQL Server DB. Upon initiation, it populates a number of columns with data from the DB. I would like to be able to edit each cell and save the results in the datagrid without posting them back to the DB table. My application is based on the tutorial found in:


Everything else works fine, I just want to eliminate updating the changes in the database.

Thanks in advance.
 
The only way to update the DB is to either write the sql and perform the update or use the sqldataadapter's update method. If you don't do either, you will see the edited results in the grid, and not update the DB.
 
That's exactly what I want to do (update the datagrid, but NOT the database).

In my update function, I have removed the Databind call, but the row still stays in edit mode (text boxes with edited data still active).

What I want is for the update function to save the changes in the datagrid and turn the "edit mode" off (i.e. EditItemIndex = -1;).
 
If you edit a cell, that cell's value is in the table rendered by the Datagrid. What additional is required? What do you mean, "save the results" in the Datagrid, but not the database? Save where? For how long? Do you wish to serialize the Datagrid object with values entered by the user?



Thomas D. Greer
 
I have three functions (EditText, UpdateText, CancelText). Calling the EditText function allows the user to alter what values are in the datagrid. The UpdateText function should turn off the "edit mode" and show the updated values in the datagrid (without texboxes in each column). The CancelText function works fine.

This should be real simple but I cannot seem to get the UpdateText function to work.
 
Here are the EditText and UpdateText functions. As I've said, they couldn't be much simpler:

Code:
		protected void EditText(object source, DataGridCommandEventArgs e) 
		{
			DropDownList LEDropDownList = (DropDownList)Page.FindControl("LEDropDownList");
			string stproc;
			string connParam;

			if(!LEDropDownList.Visible)
			{
				stproc = "[sp_Parameter_Std_Data]";
				connParam = "";
			}
			else
			{
				stproc = "[sp_Parameter_LE_Data]";
				connParam = LEDropDownList.SelectedValue;
			}
			DGDataBind(stproc,"Parameter_Std_Data","Parameter_Std_SubData",connParam);
			ParamEditGrid.EditItemIndex = e.Item.ItemIndex;
			ParamEditGrid.DataBind();
		} 

		protected void UpdateText(object source, DataGridCommandEventArgs e) 
		{
			ParamEditGrid.EditItemIndex = -1;
		}

Since I don't want to postback the edits to the database, the UpdateText function should just "turn off" the edit mode with EditItemIndex = -1. I wasn't sure If I needed to call DataBind() again. If I do, the whole datagrid disappears.
 
In order to actually set the new EditItmeIndex, you DO have to bind the grid. By re-binding the grid it does not mean it is saving to the DB, you are just binding any changes to the grid and redisplaying it, in this case with no row in edit mode.
If you are not seeing the grid after the databind, then the data in your data source (datagrid, datatable.. etc) is not able to accessed due to it's scope. For example, if in your page_load you do this:
Code:
'pseudo-code ..
   If NOT IsPostBack Then
       Dim ds as New Dataset
       Fill your dataset
       datagrid.databind
Your dataset's scope is limited to your page_load event, so when you databind in the EditText sub, there will be no rows or you would get an error.

Take a look at how and when you fill your datasource. If you have any questions, post them..

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top