I want to attach a right context menu to a RadGrid . I followed the instruction in the article:
As my data source is a collection, I have to handle the RadGrid1_InsertCommand to insert the new record into database.
But after update database, how can I turn off the grid insert mode? I put
RadGrid1.MasterTableView.IsItemInserted = false; in the RadGrid1_InsertCommand, but I received a exception:
Insert item is available only when grid is in insert mode.
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
// get sort order
RadNumericTextBox txtSortOrder =
editedItem.FindControl("RadNumericTextBox_SortOrder") as RadNumericTextBox;
int sortOrder = -1;
Int32.TryParse(txtSortOrder.ValidationText, out sortOrder);
// InsertIntoDatabase()
…
//exception: Insert item is available only when grid is in insert mode.
RadGrid1.MasterTableView.IsItemInserted = false;
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
// get category Id
Label lblId = editedItem.FindControl("lblId") as Label;
int categoryId = 0;
Int32.TryParse(lblId.Text, out categoryId);
if (0 == categoryId) return;
// update database
…
//works fine
RadGrid_Categories.EditIndexes.Clear();
}
protected void RadContectMenu1_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
{
int iClickedRowIndex;
bool convertResult = Int32.TryParse(Request.Params["ClickedRowIndex"], out iClickedRowIndex);
if (!convertResult) return;
string clickedCommand = e.Item.Text;
switch (clickedCommand)
{
case "Insert":
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid_Categories.Rebind();
break;
case "Edit":
RadGrid1.Items[iClickedRowIndex].Edit = true;
RadGrid_Categories.Rebind();
break;
}
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
GridColumn col = (GridColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
if (RadGrid_Categories.EditIndexes.Count > 0 || RadGrid_Categories.MasterTableView.IsItemInserted )
{
col.Visible = true;
}
else
{
col.Visible = false;
}
}