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

Radgrid not firing the updatecommand and insertcommand

3 Answers 285 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dang
Top achievements
Rank 1
Dang asked on 09 Jun 2011, 07:26 AM
Hi Everyone,

First of all I have to say sorry about my English. It's not my parent language.

Let's focus on the problem I have now.  I have a rad grid with the editform. I am sure that I did follow the example.

Here are the codes I have done.

Design code in my user control.
<telerik:RadGrid ID="RadGrid1" AllowAutomaticUpdates="True" runat="server"
    Width="100%" GridLines="None"
            AutoGenerateColumns="False" PageSize="13" AllowSorting="True" AllowPaging="True"
            OnUpdateCommand="RadGrid1_UpdateCommand" OnNeedDataSource="RadGrid1_NeedDataSource"
            ShowStatusBar="True" OnInsertCommand="RadGrid1_InsertCommand"
    OnDeleteCommand="RadGrid1_DeleteCommand" AllowFilteringByColumn="True"
    CellSpacing="0" onitemdatabound="RadGrid1_ItemDataBound">
            <MasterTableView DataKeyNames="ID" AllowMultiColumnSorting="True" Width="100%"
                CommandItemDisplay="Top" InsertItemDisplay="Top"
                CommandItemSettings-AddNewRecordText="Add new record"
                HierarchyLoadMode="ServerBind">
<CommandItemSettings AddNewRecordText="Add new record" ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>
 
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
                <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton"  EditImageUrl="~/RadControls/Grid/Skins/Edit.gif"  UniqueName="EditCommandColumn">
                        <HeaderStyle Width="16px"></HeaderStyle>
                        <ItemStyle Width="16px" />
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn ConfirmText="Are you sure you want to delete this record?" UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" ImageUrl="~/RadControls/Grid/Skins/Delete.gif" Text="Delete">
                    <HeaderStyle Width="16px"></HeaderStyle>
                        <ItemStyle Width="16px" /></telerik:GridButtonColumn>
                    <telerik:GridBoundColumn UniqueName="Name" SortExpression="Name" HeaderText="Document Name"
                        DataField="Name" />
                    <telerik:GridBoundColumn UniqueName="Description" HeaderText="Description" Visible="false"
                        DataField="Description" />
                    <telerik:GridDropDownColumn UniqueName="DocumentType" ListDataMember="DocumentType"
                        SortExpression="DocumentType" ListTextField="DocumentType" ListValueField="DocumentType"
                        HeaderText="Document type" DataField="DocumentType">
                    </telerik:GridDropDownColumn>
 <telerik:GridBoundColumn datafield="UpdateTime" AllowFiltering="false"
                                        DataFormatString="{0:dd MMM yyyy HH:mm:ss}" editformheadertextformat="{0}"
                                        headertext="Last Updated"
                                        readonly="True" uniquename="UpdateTime">
                                        <ItemStyle HorizontalAlign="Left" Width="160px" Wrap="False" />
                                        <HeaderStyle HorizontalAlign="Left" Width="160px" Wrap="False" />
                                    </telerik:GridBoundColumn>
                </Columns>
                <EditFormSettings  CaptionFormatString="Edit record : {0}" CaptionDataField="Name">
                    <FormTableItemStyle Width="100%" Height="29px"></FormTableItemStyle>
                    <FormTableStyle GridLines="None" CellSpacing="0" CellPadding="2"></FormTableStyle>
                    <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
                    <EditColumn ButtonType="ImageButton"  />
                     
                </EditFormSettings>
            </MasterTableView>
<FilterMenu EnableImageSprites="False"></FilterMenu>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
        </telerik:RadGrid>



And below is my code behind.
protected void Page_Load(object sender, EventArgs e)
       {
 
       }
       public void Initialize(int iProjectID)
       {
           RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms;
           this.hdProjectID.Value = iProjectID.ToString();
           this.RadGrid1.DataBind();
            
       }
 
       protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
       {
           GridEditableItem editedItem = 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 editorType = 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 GridDropDownColumnEditor)
                       {
                           editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " +
                               (editor as GridDropDownColumnEditor).SelectedValue;
                           editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                       }
 
                       try
                       {
                           int iDataID = (int)editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"];
                           Data oData = new Data(iDataID, this.UserID);
                           bool bUpdated = false;
                           switch(column.UniqueName)
                           {
                               case "Name":
                                   oData.Name = editorValue.ToString();
                                   bUpdated = true;
                                   break; 
                               case "Description":
                                   oData.Description = editorValue.ToString();
                                   bUpdated = true;
                                   break;
                               case "DataType":
                                   oData.TypeID = Convert.ToInt32(editorValue.ToString());
                                   break;
                           }
                           if (bUpdated)
                           {
                             //
                           }
                       }
                       catch (Exception ex)
                       {
                           RadGrid1.Controls.Add(new LiteralControl("<strong>Unable to update'" + column.HeaderText + "' value.</strong> - " + ex.Message));
                           e.Canceled = true;
                           break;
                       }
                   }
               }
           }
       }
 
 
       protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
       {
           Datas oDatas = new Datas(Convert.ToInt32(this.hdProjectID.Value), this.UserID, true);
           this.RadGrid1.DataSource = oDatas;
       }
       protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
       {
           switch (e.CommandName)
           {
               case "":
                   if (RadGrid1.EditIndexes.Count == 0)
                   {
                       return;
                   }
 
                   foreach (GridDataItem item in RadGrid1.EditItems)
                   {
                       GridEditableItem itemToEdit =
                       (item.OwnerTableView.EditMode == GridEditMode.InPlace) ?
                       (GridEditableItem)item : (GridEditableItem)item.EditFormItem;
                       //UpdateItem(itemToEdit);
                   }
 
                   e.Item.OwnerTableView.Rebind();
                   break;
           }
       }
       protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
       {
           GridEditableItem editedItem = e.Item as GridEditableItem;
           GridEditManager editMan = editedItem.EditManager;
           Datas oDatas = new Datas(Convert.ToInt32(this.hdProjectID.Value), this.UserID, true);
           Data oDoc = oDatas.Add(1);
           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 GridDropDownColumnEditor)
                       {
                           editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " +
                            (editor as GridDropDownColumnEditor).SelectedValue;
                           editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                       }
 
                       try
                       {
                           switch (column.UniqueName)
                           {
                               case "Name":
                                   oDoc.Name = editorValue.ToString();
                                   break;
                               case "Description":
                                   break;
                               case "DataType":
                                   break;
 
                           }
                         
                       }
                       catch (Exception ex)
                       {
                           RadGrid1.Controls.Add(new LiteralControl("Unable to add new Data. Reason: " + ex.Message));
                           e.Canceled = true;
                       }
 
                   }
               }
           }
       }
       protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
       {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
               GridEditableItem editedItem = e.Item as GridEditableItem;
               GridEditManager editMan = editedItem.EditManager;
               GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("DataType"));
               DropDownList ddList = editor.DropDownListControl;
               DataTypeList oDTList = new DataTypeList();
               ddList.DataSource = oDTList;
               ddList.DataBind();
           }
       }
       protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
       {
           int ID = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
           Datas oDatas = new Datas(Convert.ToInt32(this.hdProjectID.Value), this.UserID, true);
           oDatas.Remove(ID);
       }


Just in case you guys want to know what kind of datatype that I'm binding it to the grid. It's the arraylist.


Could you please tell where in the code that I miss ? Right now when I click on the "Insert new record " It show me the edit form . But once I click on the update button . There is nothing happen.


Thank you so much.


Dang

3 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 13 Jun 2011, 04:07 PM
Hello Dang,

I am not sure where you are calling the Initialize method, but you should remove the call to the grid's DataBind method in it. Furthermore, remove the call to the OwnerTableView's Rebind() method as well from the grid events. And last but not least, once you are in the Insert/Update command event use the e.Item.ExtractValues method to get the column field values that you should persist to the underlying data-source for the grid. 

Hope it helps. 

Best wishes,
Tsvetoslav
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Dang
Top achievements
Rank 1
answered on 16 Jun 2011, 06:56 AM
Hi Tsvetoslav

i really appreciated your reply.  

Just to let you know that I tried everything you mentioned.  Unfortunately it didn't work.

I got stuck with this problem for many days but eventually I got it fixed.

I'm not sure that it's the right way to do it or not but I removed the rad ajax from the radgrid control .

it works perfectly now. The only one problem i have left is without ajax thingy . The grid doesn't work beautifully when I press edit  , delete , insert and drag and drop. It keeps jumping around which is not nice.

Do you know how to get it to work with radajax by this binding method condition that I'm doing.

Many thanks.

Dang.
0
Tsvetoslav
Telerik team
answered on 17 Jun 2011, 03:31 PM
Hi Dang,

Just wrap the RadGrid control within a RadAjaxPanel - that should make the grid's requests asynchronous.

Hope it helps. 

Greetings,
Tsvetoslav
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Dang
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Dang
Top achievements
Rank 1
Share this question
or