display of the page(not insert the database), then click the "save" button outside
of the RadGrid ,then all the temporary display of data saved to the database?
Do you have example like this?or talk about it Detailed?
Thank you !!
3 Answers, 1 is accepted
One suggestion would be to store the Data from the Database to a Session and bind the Grid from the Session. So once you do Insert/Update/Delete operation only the session will be affected and not the database. And in the click event of the save button update the DB from the session, clear the session and Rebind the Grid.
CS:
public DataSet EmployeesData |
{ |
get |
{ |
object obj = this.Session["EmployeesData"]; |
if (obj != null) |
{ |
return (DataSet)obj; |
} |
DataSet employeesData = new DataSet(); |
String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; |
SqlConnection conn = new SqlConnection(ConnString); |
SqlDataAdapter adapter = new SqlDataAdapter(); |
adapter.SelectCommand = new SqlCommand("SELECT EmployeeID, FirstName, LastName, TitleOfCourtesy, HireDate FROM Employees", conn); |
adapter.Fill(employeesData, "Employees"); |
this.Session["EmployeesData"] = employeesData; |
return employeesData; |
} |
} |
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) |
{ |
thisthis.RadGrid1.DataSource = this.EmployeesData; |
this.EmployeesData.Tables["Employees"].PrimaryKey = new DataColumn[] { this.EmployeesData.Tables["Employees"].Columns["EmployeeID"] }; |
} |
protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
{ |
GridEditableItem eeditedItem = e.Item as GridEditableItem; |
GridEditManager editMan = editedItem.EditManager; |
foreach( GridColumn column in e.Item.OwnerTableView.RenderColumns ) |
{ |
if ( column is IGridEditableColumn ) |
{ |
IGridEditableColumn editableCol = (column as IGridEditableColumn); |
if ( editableCol.IsEditable ) |
{ |
IGridColumnEditor editor = editMan.GetColumnEditor( editableCol ); |
string editoreditorType = editor.ToString(); |
string editorText = "unknown"; |
object editorValue = null; |
if ( editor is GridTextColumnEditor ) |
{ |
editorText = (editor as GridTextColumnEditor).Text; |
editorValue = (editor as GridTextColumnEditor).Text; |
} |
if ( editor is GridBoolColumnEditor ) |
{ |
editorText = (editor as GridBoolColumnEditor).Value.ToString(); |
editorValue = (editor as GridBoolColumnEditor).Value; |
} |
if ( editor is GridDropDownColumnEditor ) |
{ |
editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " + |
(editor as GridDropDownColumnEditor).SelectedValue; |
editorValue = (editor as GridDropDownColumnEditor).SelectedValue; |
} |
try |
{ |
DataRow[] changedRows = this.EmployeesData.Tables["Employees"].Select( "EmployeeIDEmployeeID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"] ); |
changedRows[0][column.UniqueName] = editorValue; |
this.EmployeesData.Tables["Employees"].AcceptChanges(); |
} |
catch(Exception ex) |
{ |
RadGrid1.Controls.Add(new LiteralControl ("<strong>Unable to set value of column '" + column.UniqueName + "'</strong> - " + ex.Message)); |
e.Canceled = true; |
break; |
} |
} |
} |
} |
} |
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e) |
{ |
GridEditableItem eeditedItem = e.Item as GridEditableItem; |
GridEditManager editMan = editedItem.EditManager; |
DataTable employeesTable = this.EmployeesData.Tables["Employees"]; |
DataRow newRow = employeesTable.NewRow(); |
//Set new values |
foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns) |
{ |
if (column is IGridEditableColumn) |
{ |
IGridEditableColumn editableCol = (column as IGridEditableColumn); |
if (editableCol.IsEditable) |
{ |
IGridColumnEditor editor = editMan.GetColumnEditor(editableCol); |
string editorText = "unknown"; |
object editorValue = null; |
if (editor is GridTextColumnEditor) |
{ |
editorText = (editor as GridTextColumnEditor).Text; |
editorValue = (editor as GridTextColumnEditor).Text; |
} |
if (editor is GridBoolColumnEditor) |
{ |
editorText = (editor as GridBoolColumnEditor).Value.ToString(); |
editorValue = (editor as GridBoolColumnEditor).Value; |
} |
if (editor is GridDropDownColumnEditor) |
{ |
editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " + |
(editor as GridDropDownColumnEditor).SelectedValue; |
editorValue = (editor as GridDropDownColumnEditor).SelectedValue; |
} |
try |
{ |
newRow[column.UniqueName] = editorValue; |
} |
catch (Exception ex) |
{ |
RadGrid1.Controls.Add(new LiteralControl("Unable to insert into Employees. Reason: " + ex.Message)); |
e.Canceled = true; |
} |
} |
} |
} |
//As this example demonstrates only in-memory editing, a new primary key value should be generated |
//This should not be applied when updating directly the database |
newRow["EmployeeID"] = (int)employeesTable.Rows[employeesTable.Rows.Count - 1]["EmployeeID"] + 1; |
employeesTable.Rows.Add(newRow); |
this.EmployeesData.Tables["Employees"].AcceptChanges(); |
} |
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e) |
{ |
string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"].ToString(); |
DataTable employeeTable = this.EmployeesData.Tables["Employees"]; |
if (employeeTable.Rows.Find(ID) != null) |
{ |
employeeTable.Rows.Find(ID).Delete(); |
employeeTable.AcceptChanges(); |
} |
} |
} |
//Save Button
protected void Button1_Click1(object sender, EventArgs e) |
{ |
//Write the code to Update the DataBase from the EmployeesData DataSet |
Session.Clear(); |
RadGrid1.Rebind(); |
} |
Thanks
Princy.
Hello Princy:
I understand, Insert sucessful, but when I delete the provisional data accrossing your meaning, I have a mistake,"Table not have primary key", in fact , I set the dataKeyNames in the Front_code, This my behind_code below:
protected void rgTemplate_DeleteCommand(object source, GridCommandEventArgs e)
{
string customsCode = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CUSTOMS_CODE"].ToString();
DataTable templateTable = this.TemplateTable.Tables["TEMPLATE_TABLE"];
if (templateTable.Rows.Find(customsCode) != null) //this is a mistake,"table not have a primary key"
{
templateTable.Rows.Find(customsCode).Delete();
templateTable.AcceptChanges();
}
}
Where I have a mistake??Thank you!