Hi,
I'm using a demo call update/insert/delete when the EnableViewState property of the grid is set to false.
If i press cancel button ni editform anyway insert record.
Wha can i do?
Regards...
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.UpdateCommandName)
{
RadGrid1.DataSource =
null;
}
else if (e.CommandName == RadGrid.EditCommandName)
{
Session[
"initEdit"] = true;
Session[
"updateFlag"] = true;
}
else if (e.CommandName == RadGrid.InitInsertCommandName)
{
Session[
"insertFlag"] = true;
}
else if(e.CommandName == RadGrid.DeleteCommandName)
{
string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"].ToString();
DataTable employeesTable = (DataTable)Session["employeesTable"];
if (employeesTable.Rows.Find(ID) != null)
{
employeesTable.Rows.Find(ID).Delete();
employeesTable.AcceptChanges();
Session[
"employeesTable"] = employeesTable;
}
}
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
if(Session["updateFlag"] != null && Session["initEdit"] != null)
{
DataTable employeesTable = (DataTable)Session["employeesTable"];
for (int i = 0; i < RadGrid1.EditIndexes.Count; i++)
{
GridDataItem dataItem = RadGrid1.MasterTableView.Items[RadGrid1.EditIndexes[i]] as GridDataItem;
GridEditableItem editItem = (dataItem.OwnerTableView.EditMode == GridEditMode.InPlace) ? (GridEditableItem)dataItem : (GridEditableItem)dataItem.EditFormItem;
GridEditManager mng = editItem.EditManager;
foreach (GridColumn column in dataItem.OwnerTableView.Columns)
{
if (column is IGridEditableColumn)
{
IGridEditableColumn editedColumn = column as IGridEditableColumn;
IGridColumnEditor editor = mng.GetColumnEditor(editedColumn);
string editorType = editor.ToString();
string editorText = null;
if (editor is GridTextColumnEditor)
{
editorText = (editor
as GridTextColumnEditor).Text;
}
try
{
DataRow[] changedRows = employeesTable.Select("EmployeeID = " + editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["EmployeeID"]);
changedRows[0][column.UniqueName] = editorText;
}
catch (Exception ex)
{
RadGrid1.Controls.Add(
new LiteralControl("Unable to update Employee. Reason: " + ex.Message));
}
}
}
}
employeesTable.AcceptChanges();
Session[
"employeesTable"] = employeesTable;
Session[
"updateFlag"] = false;
Session[
"editIndex"] = null;
}
else if (Session["insertFlag"] != null)
{
DataTable employeesTable = (DataTable)Session["employeesTable"];
DataRow newRow = employeesTable.NewRow();
GridEditableItem insertItem = RadGrid1.MasterTableView.GetInsertItem();
GridEditManager mng = insertItem.EditManager;
foreach (GridColumn column in insertItem.OwnerTableView.Columns)
{
if (column is IGridEditableColumn)
{
IGridEditableColumn editedColumn = column as IGridEditableColumn;
IGridColumnEditor editor = mng.GetColumnEditor(editedColumn);
string editorType = editor.ToString();
string editorText = null;
if (editor is GridTextColumnEditor)
{
editorText = (editor
as GridTextColumnEditor).Text;
}
try
{
newRow[column.UniqueName] = editorText;
}
catch (Exception ex)
{
RadGrid1.Controls.Add(
new LiteralControl("Unable to insert into Employees. Reason: " + ex.Message));
}
}
}
//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);
Session[
"employeesTable"] = employeesTable;
Session[
"insertFlag"] = null;
}
Session[
"initEdit"] = null;
RadGrid1.DataSource = (
DataTable)Session["employeesTable"];
}