This is a migrated thread and some comments may be shown as answers.

[Solved] When i press button close in editform anyway insert record.

1 Answer 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alfa
Top achievements
Rank 1
Alfa asked on 12 Oct 2009, 03:54 PM

 

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"];

 

}

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 15 Oct 2009, 04:03 PM
Hello Francisco,

Please refer to my reply in the code library thread where you posted the same question:
http://www.telerik.com/community/code-library/aspnet-ajax/grid/update-insert-delete-in-datasourcepersistencemode-nopersistence.aspx

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Alfa
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or