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

Convert columns noneditable in Edit mode to editable in Insert mode

9 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RB
Top achievements
Rank 1
RB asked on 25 Mar 2014, 09:51 PM
I have a set of columns which are not editable in 'Edit Mode'. But when on 'Insert Mode' I want the same columns to have dropdowns and I should be able to select values. In EditMode to disable these columns this is what I have done:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
       {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode && !(e.Item is GridEditFormInsertItem))
           {
 
               GridEditableItem item = e.Item as GridEditableItem;
               GridEditManager manager = item.EditManager;
               //Adding columns that are not editable
               GridTextBoxColumnEditor editor = manager.GetColumnEditor("Description") as GridTextBoxColumnEditor;
               editor.TextBoxControl.Enabled = false;
               editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
               editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
               editor = manager.GetColumnEditor("SameRegion") as GridTextBoxColumnEditor;
               editor.TextBoxControl.Enabled = false;
               editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
               editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
               editor = manager.GetColumnEditor("AVRMName") as GridTextBoxColumnEditor;
               editor.TextBoxControl.Enabled = false;
               editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
               editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
               editor = manager.GetColumnEditor("AVRMZName") as GridTextBoxColumnEditor;
               editor.TextBoxControl.Enabled = false;
               editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
               editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
           }
                 
       }
The description and SameRegion columns should have a dropdown in 'InsertMode' so that I can pass the selected values. How can I achieve this?

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Mar 2014, 09:25 AM
Hi,

Please try the following code snippet to have a DropDownList for a column in insert mode and hide it during edit.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  // Create DropDownList in insert mode
    if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        TextBox txt = (TextBox)edit["SameRegion"].Controls[0];
        txt.Visible = false;
        DropDownList drop = new DropDownList();
        drop.ID = "DropDownList1";
        drop.DataSourceID = "SqlDataSource2";
        drop.DataTextField = "SameRegion";
        drop.DataValueField = "SameRegion";
        edit["SameRegion"].Controls.Add(drop);      
    }
  //  Hide column in edit mode
    if (e.Item is GridEditableItem && !e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        edit["SameRegion"].Parent.Visible = false;
    }
}

Thanks,
Shinu
0
RB
Top achievements
Rank 1
answered on 26 Mar 2014, 10:13 PM
Thanks for the reply. No matter what, on Update I get old grid Values and not the newValues.It was working fine till I guess I messed some part!
Instead of getting new values selected from the dropdownlist, i get the previous ones on Update!
public class OrderRequestOrderEntrySystemListCtrl : WebControl, INamingContainer
{ 
    #region Fields
    private L3RadGridTitleBar _L3RadGridTitleBar;
    private Telerik.Web.UI.RadGrid _RadGrid1 = new  Telerik.Web.UI.RadGrid();
    private RadAjaxLoadingPanel _RadLoadingPanel = new RadAjaxLoadingPanel();
    private UpdatePanel _UpdatePanel = new UpdatePanel();
      
    private CatalogProductRegionOrderEntryItemTable _CatalogProductRegionOrderEntryItemTable =     new CatalogProductRegionOrderEntryItemTable();
    private AvrmQueueTypeTable _AvrmQueueTypeTable = new AvrmQueueTypeTable();
    private CatalogProductTable _CatalogProductTable = new CatalogProductTable();              
  
    #endregion
  
    #region Constructors
  
    public OrderRequestOrderEntrySystemListCtrl() { }
    #endregion
  
    #region Overrides
  
    protected override void OnInit(EventArgs e)
    {               
        this._RadLoadingPanel.ID = "_RadLoadingPanel";
        this._RadLoadingPanel.Transparency = 30;
        this._RadLoadingPanel.Skin = "WebBlue";
        this._RadLoadingPanel.BackgroundPosition = AjaxLoadingPanelBackgroundPosition.TopRight;
        this.Controls.Add(this._RadLoadingPanel);
  
        this._L3RadGridTitleBar = new L3RadGridTitleBar(_RadGrid1);
        this._L3RadGridTitleBar.ID = "_L3RadGridTitleBar";
        this._L3RadGridTitleBar.PageOrientation = Orientation.Vertical;
        this._L3RadGridTitleBar.CellAlignment = System.Drawing.ContentAlignment.MiddleLeft;
        this._L3RadGridTitleBar.ShowExportXls = true;
        this.Controls.Add(this._L3RadGridTitleBar);
      
        this._RadGrid1.Skin = "WebBlue";
        this._RadGrid1.Width = Unit.Percentage(98);
        this._RadGrid1.GridLines = GridLines.None;
        this._RadGrid1.PageSize = 100;
        this._RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
  
        this._RadGrid1.AllowPaging = true;
        this._RadGrid1.AllowSorting = true;
        this._RadGrid1.AllowFilteringByColumn = true;
        this._RadGrid1.AllowAutomaticDeletes = true;
        this._RadGrid1.AllowAutomaticInserts = true;
        this._RadGrid1.AllowAutomaticUpdates = true; 
        this._RadGrid1.AutoGenerateColumns = false;
        this._RadGrid1.AutoGenerateEditColumn = true;
        this._RadGrid1.AutoGenerateDeleteColumn = true;
        this._RadGrid1.ClientSettings.DataBinding.EnableCaching = true;
        this._RadGrid1.ClientSettings.AllowGroupExpandCollapse = true;      
       
        this._RadGrid1.ClientSettings.AllowKeyboardNavigation = true;
         
        this._RadGrid1.EnableGroupsExpandAll = true;
        this._RadGrid1.EnableLinqExpressions = false;
        this._RadGrid1.EnableHeaderContextFilterMenu = true;
        this._RadGrid1.GroupingEnabled = true;
        this._RadGrid1.ShowGroupPanel = true;
          
        this._RadGrid1.MasterTableView.GroupLoadMode = GridGroupLoadMode.Client;
        this._RadGrid1.MasterTableView.Width = Unit.Percentage(100);
        this._RadGrid1.MasterTableView.EnableHeaderContextMenu = true;
        this._RadGrid1.MasterTableView.EnableHeaderContextFilterMenu = true;
        this._RadGrid1.MasterTableView.NoMasterRecordsText = "There are no Order Entry System Settings";
        this._RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
        this._RadGrid1.MasterTableView.DataKeyNames = new string[] {this._CatalogProductRegionOrderEntryItemTable.CatalogProductRegionOrderEntryIdColumn.ColumnName};
        this._RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        this._RadGrid1.MasterTableView.CommandItemSettings.AddNewRecordText = "Add Order Entry Setting";
        this._RadGrid1.MasterTableView.InsertItemPageIndexAction = GridInsertItemPageIndexAction.ShowItemOnCurrentPage;
          
  
        //this._RadGrid1.MasterTableView.DetailItemTemplate = new OrderRequestOESTemplate();
        this._RadGrid1.ItemCreated += RadGrid1_ItemCreated;
        this._RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
        this._RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace;
                      
        //this._RadGrid1.ItemInserted += RadGrid1_ItemInserted;
        this._RadGrid1.ItemCommand += _RadGrid1_ItemCommand;
       // this._RadGrid1.EditCommand += _RadGrid1_EditCommand;
        this._RadGrid1.DeleteCommand += _RadGrid1_DeleteCommand;         
        this._RadGrid1.UpdateCommand += RadGrid1_UpdateCommand;
        this._RadGrid1.InsertCommand += _RadGrid1_InsertCommand;
  
        this._RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
  
        LoadList();
      
        #region Columns
        String columnName;
  
        columnName = this._CatalogProductRegionOrderEntryItemTable.DescriptionColumn.ColumnName;
        GridBoundColumn boundColumn = new GridBoundColumn();
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.FilterTemplate = new DropdownTemplate(columnName, _RadGrid1, this._CatalogProductTable.SelectDistinct(columnName));
        boundColumn.DataField = columnName;
        boundColumn.HeaderText = "Product";
        boundColumn.UniqueName = this._CatalogProductRegionOrderEntryItemTable.DescriptionColumn.ColumnName;
        boundColumn.Groupable = true;      
         
        //boundColumn = new GridBoundColumn();
        //this._RadGrid1.MasterTableView.Columns.Add(boundColumn);
        //boundColumn.DataField = this._CatalogProductRegionOrderEntryItemTable.ProductCodeColumn.ColumnName;
        //boundColumn.HeaderText = "Product Code";
        //boundColumn.UniqueName = this._CatalogProductRegionOrderEntryItemTable.ProductCodeColumn.ColumnName;
        //boundColumn.Groupable = true;
        //boundColumn.AllowFiltering = true;               
  
        columnName = this._CatalogProductRegionOrderEntryItemTable.SourceSystemTypeColumn.ColumnName;
        boundColumn = new GridBoundColumn();
        boundColumn.FilterTemplate = new DropdownTemplate(columnName, _RadGrid1, this._CatalogProductRegionOrderEntryItemTable.SelectDistinct(columnName));
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.DataField = columnName;
        boundColumn.AllowFiltering = true;
        boundColumn.Groupable = true;
        boundColumn.AutoPostBackOnFilter = true;
        boundColumn.HeaderText = "Order Entry System";
        boundColumn.UniqueName = columnName;
        boundColumn.AllowSorting = true;          
  
        boundColumn = new   GridBoundColumn();
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.DataField = this._CatalogProductRegionOrderEntryItemTable.IsALLRegionsTheSameColumn.ColumnName;
        boundColumn.HeaderText = "Same Region";
        boundColumn.AllowSorting = true;
        boundColumn.Groupable = true;
        boundColumn.UniqueName = "SameRegion";                   
  
        columnName = this._CatalogProductRegionOrderEntryItemTable.AVRMQueueColumn.ColumnName; ;
        boundColumn = new GridBoundColumn();
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.FilterTemplate = new DropdownTemplate(columnName, this._RadGrid1, this._AvrmQueueTypeTable);
        boundColumn.DataField = columnName;
        boundColumn.HeaderText = "AVRM";
        boundColumn.AllowFiltering = true;
        boundColumn.Groupable = true;
        boundColumn.UniqueName = "AVRM";
  
        columnName = this._CatalogProductRegionOrderEntryItemTable.AvrmDisplayColumn.ColumnName;
        boundColumn = new GridBoundColumn();
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);          
        boundColumn.DataField = columnName;
        boundColumn.HeaderText = "AVRM Name";
        boundColumn.AllowFiltering = false;
        boundColumn.UniqueName = "AVRMName";         
          
        columnName = this._CatalogProductRegionOrderEntryItemTable.AVRMQueueZColumn.ColumnName;          
        boundColumn = new GridBoundColumn();
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn.FilterTemplate = new DropdownTemplate(columnName, this._RadGrid1, this._CatalogProductRegionOrderEntryItemTable.SelectDistinct(columnName));
        boundColumn.DataField = columnName;
        boundColumn.HeaderText = "AVRM Z";
        boundColumn.AllowFiltering = true;
        boundColumn.Groupable = true;
        boundColumn.UniqueName = "AVRMZ";
  
        columnName = this._CatalogProductRegionOrderEntryItemTable.AvrmDisplayZColumn.ColumnName;
        boundColumn = new GridBoundColumn();
        this._RadGrid1.MasterTableView.Columns.Add(boundColumn);          
        boundColumn.DataField = columnName;
        boundColumn.HeaderText = "AVRM Z Name";
        boundColumn.AllowFiltering = false;
        boundColumn.UniqueName = "AVRMZName";
          
        #endregion
                         
        this._UpdatePanel.ID = "ajaxPanel";
        this._UpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
        this._UpdatePanel.ContentTemplateContainer.Controls.Add(this._RadGrid1);
  
        this.Controls.Add(this._UpdatePanel);
  
        base.OnInit(e);
    }
  
  
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this._RadGrid1.EditIndexes.Add(0);
        }
    }
  
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            if (!(e.Item is GridEditFormInsertItem))
            {
                GridEditableItem item = e.Item as GridEditableItem;
                GridEditManager manager = item.EditManager;
                //Adding columns that are not editable
                GridTextBoxColumnEditor editor = manager.GetColumnEditor("Description") as GridTextBoxColumnEditor;
                editor.TextBoxControl.Enabled = false;
                editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
                editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
                editor = manager.GetColumnEditor("SameRegion") as GridTextBoxColumnEditor;
                editor.TextBoxControl.Enabled = false;
                editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
                editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
                editor = manager.GetColumnEditor("AVRMName") as GridTextBoxColumnEditor;
                editor.TextBoxControl.Enabled = false;
                editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
                editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
                editor = manager.GetColumnEditor("AVRMZName") as GridTextBoxColumnEditor;
                editor.TextBoxControl.Enabled = false;
                editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
                editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
            }  
            //else if(e.Item.ItemIndex<0)
            //{
            //    GridEditableItem item = e.Item as GridEditableItem;
            //    GridEditManager manager = item.EditManager;
            //     GridTextBoxColumnEditor editor = manager.GetColumnEditor("AVRMName") as GridTextBoxColumnEditor;
            //    editor.TextBoxControl.Enabled = false;
            //    editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
            //    editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
            //    editor = manager.GetColumnEditor("AVRMZName") as GridTextBoxColumnEditor;
            //    editor.TextBoxControl.Enabled = false;
            //    editor.TextBoxControl.BackColor = System.Drawing.Color.Gray;
            //    editor.TextBoxControl.ForeColor = System.Drawing.Color.Black;
            //}
        }
    }
  
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        String SourceSystemDescription = "", AVRMQ = "", AVRMZ = "";
        string[] UniqueName = new string[3] { "SourceSystemType", "AVRM", "AVRMZ" };
        string[] DBColName = new string[3]  {"SourceSystemType", "AVRMQueue", "AVRMQueueZ"};
        
        if (e.Item is GridEditableItem && e.Item.IsInEditMode && !(e.Item is GridEditFormInsertItem) && !(e.Item.ItemIndex<0))
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;       
            int CatalogProductRegionOEID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CatalogProductRegionOrderEntryId"]);
            DataRow[] changedRows = _CatalogProductRegionOrderEntryItemTable.Select("CatalogProductRegionOrderEntryId = " + CatalogProductRegionOEID);
              
            Hashtable oldValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(oldValues, editedItem);
  
            DataRow changedRow = changedRows[0];
  
            foreach (DictionaryEntry entry in oldValues)
            {
                changedRow[(string)entry.Key] = entry.Value;
                if (entry.Key.Equals("SourceSystemType"))
                    SourceSystemDescription = entry.Value.ToString();
                if (entry.Key.Equals("AVRMQueue"))
                    AVRMQ = string.Format("{0}", entry.Value);
                if (entry.Key.Equals("AVRMQueueZ") && (entry.Value != null))
                    AVRMZ = string.Format("{0}", entry.Value);
            }
          
            for (int count = 0; count < UniqueName.Length; count++)
            {
                TextBox txt = (TextBox)editedItem[UniqueName[count]].Controls[0];
                txt.Visible = false;
  
                DropDownList droplist = new DropDownList();
                droplist.DataSource = this._CatalogProductRegionOrderEntryItemTable.SelectDistinct(DBColName[count]);
                if (UniqueName[count].Equals("AVRM"))
                {
                    droplist.DataSource = this._AvrmQueueTypeTable;
                    droplist.Text = AVRMQ;
                }
                else if(UniqueName[count].Equals("AVRMZ"))
                    droplist.Text = AVRMZ;
                else if (UniqueName[count].Equals("SourceSystemType"))
                    droplist.Text = SourceSystemDescription;                     
                droplist.DataTextField = DBColName[count];
                droplist.DataValueField = DBColName[count];
             //   droplist.DataBind();
                editedItem[UniqueName[count]].Controls.Add(droplist);
            }
        }
    }
    void _RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {         
        GridEditableItem editedItem = e.Item as GridEditableItem;
        DataTable ordersTable = this._CatalogProductRegionOrderEntryItemTable;
        DataRow newRow = ordersTable.NewRow(); 
    }
  
    void _RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        String SourceSystemDescription = "", AVRMQ = "", AVRMZ = "", Product = "";
        bool IsAllRegionsTheSame = false;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        //DataTable CPROETable = this._CatalogProductRegionOrderEntryItemTable;
        //Create new row in the DataSource
        DataRow newRow = this._CatalogProductRegionOrderEntryItemTable.NewRow();
  
        //Insert new values
        Hashtable newValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
        try
        {
            foreach (DictionaryEntry entry in newValues)
            {
                newRow[(string)entry.Key] = entry.Value;
                if (entry.Key.Equals("SourceSystemType"))
                    SourceSystemDescription = entry.Value.ToString();
                if (entry.Key.Equals("AVRMQueue"))
                    AVRMQ = string.Format("{0}", entry.Value);
                if (entry.Key.Equals("AVRMQueueZ"))
                    AVRMZ = string.Format("{0}", entry.Value);
                if (entry.Key.Equals("Description"))
                    Product = string.Format("{0}", entry.Value);
                if (entry.Key.Equals("IsAllRegionsTheSame"))
                    IsAllRegionsTheSame =  Convert.ToBoolean(entry.Value);
            }
  
            CatalogProductRegionOrderEntry_Add _CatalogProductRegionOrderEntry_Add = new CatalogProductRegionOrderEntry_Add();
            _CatalogProductRegionOrderEntry_Add.ExecuteNonQuery(Product, SourceSystemDescription,IsAllRegionsTheSame, AVRMQ, AVRMZ);
        }
        catch (Exception ex)
        {             
            this._RadGrid1.Controls.Add(new LiteralControl("Unable to update the data. Reason: " + ex.Message));
            e.Canceled = true;
        }
  
    }
    void _RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        CatalogProductRegionOrderEntry_DeleteById _CatalogProductRegionOrderEntry_DeleteById = new CatalogProductRegionOrderEntry_DeleteById();
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int Id = 0;          
        // determine the ID
        Id = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CatalogProductRegionOrderEntryId"]);         
        _CatalogProductRegionOrderEntry_DeleteById.ExecuteNonQuery(Id);
        Page.RegisterRequiresPostBack(this._RadGrid1);
    }
  
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        String SourceSystemDescription = "", AVRMQ = "", AVRMZ = "";
        int CatalogProductRegionOEID;
        GridEditableItem editedItem = e.Item as GridEditableItem;
  
        // determine the ID
        CatalogProductRegionOEID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CatalogProductRegionOrderEntryId"]);
  
        DataRow[] changedRows = _CatalogProductRegionOrderEntryItemTable.Select("CatalogProductRegionOrderEntryId = " + CatalogProductRegionOEID);
          
        Hashtable newValues = new Hashtable();
        //The GridTableView will fill the values from all editable columns in the hash
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
  
        DataRow changedRow = changedRows[0];
        changedRow.BeginEdit();
        try
        {
            foreach (DictionaryEntry entry in newValues)
            {
                changedRow[(string)entry.Key] = entry.Value;
                if (entry.Key.Equals("SourceSystemType"))
                    SourceSystemDescription = entry.Value.ToString();
                if (entry.Key.Equals("AVRMQueue"))                      
                        AVRMQ = string.Format("{0}",entry.Value);                     
                if (entry.Key.Equals("AVRMQueueZ") && (entry.Value != null))
                    AVRMZ = string.Format("{0}", entry.Value);
            }
            changedRow.EndEdit();
            CatalogProductRegionOrderEntry_Update _CatalogProductRegionOrderEntryUpdate = new CatalogProductRegionOrderEntry_Update();
            _CatalogProductRegionOrderEntryUpdate.ExecuteNonQuery(SourceSystemDescription, AVRMQ, AVRMZ, CatalogProductRegionOEID);
        }
  
        catch (Exception ex)
        {
            changedRow.CancelEdit();
            this._RadGrid1.Controls.Add(new LiteralControl("Unable to update the data. Reason: " + ex.Message));
            e.Canceled = true;
        }
        this.Page.RegisterRequiresPostBack(this._RadGrid1);
        this._RadGrid1.MasterTableView.Rebind();      
             
    }
    
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(Page);
        ajaxManager.AjaxSettings.AddAjaxSetting(this._UpdatePanel, this._RadGrid1, this._RadLoadingPanel);        
    }
  
    #endregion Overrides
  
    #region Utility Methods
  
    void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        LoadList();
       this._RadGrid1.DataSource = this._CatalogProductRegionOrderEntryItemTable;
    }
  
    private void LoadList()
    {
        if (this._CatalogProductRegionOrderEntryItemTable.Count < 1)
            this._CatalogProductRegionOrderEntryItemTable = new CatalogProductRegionOrderEntry_List().ExecuteTypedDataTable();
        _AvrmQueueTypeTable = new AvrmQueue_List().ExecuteTypedDataTable();
        _CatalogProductTable = new CatalogProduct_List().ExecuteTypedDataTable();
    }
  
    #endregion Utility Methods
  
}
0
RB
Top achievements
Rank 1
answered on 26 Mar 2014, 11:22 PM
I checked that the problem is arising because in the ItemDataBound I am creating dropdown list. When I remove this part it works fine. I want a dropdown on click of edit for these columns.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 27 Mar 2014, 04:32 AM
Hi,

I guess you want to have a dropdown in edit mode and access it values on Update. Please try the following code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && !e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
  {
      GridEditableItem edit = (GridEditableItem)e.Item;
      DropDownList drop = (DropDownList)edit.FindControl("DropDownList1");
      drop.DataSourceID = "SqlDataSource1";
      drop.DataTextField = "SameRegion";
      drop.DataValueField = "SameRegion";
  }
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
   if (e.Item is GridEditableItem && !e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
    {
      GridEditableItem edit = (GridEditableItem)e.Item;
      TextBox txt = (TextBox)edit["SameRegion"].Controls[0];
      txt.Visible = false;
      DropDownList drop = new DropDownList();
      drop.ID = "DropDownList1";
      drop.DataSourceID = "SqlDataSource1";
      drop.DataTextField = "SameRegion";
      drop.DataValueField = "SameRegion";
      edit["SameRegion"].Controls.Add(drop);
    }
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
  GridEditableItem editedItem = (GridEditableItem)e.Item;
  DropDownList drop = (DropDownList)editedItem.FindControl("DropDownList1");
  string value = drop.SelectedValue; 
}

Thanks,
Shinu
0
RB
Top achievements
Rank 1
answered on 31 Mar 2014, 10:11 PM
Thanks Shinu. It worked. But when I try to find the dropdownlist in  _RadGrid1_EditCommand, it is unable ti find it. On click of Edit, I want the existing value to be the selected value in each dropdown. 

private void _RadGrid1_EditCommand(object sender, GridCommandEventArgs e)
{ 
    GridEditableItem editedItem = e.Item as GridEditableItem;
   DropDownList drop = new DropDownList();
   drop = (DropDownList)editedItem.FindControl("DropDownList") ;
}

On UpdateComand it is able to find the control but not on edit. Can you please tell me why this happens?
0
Shinu
Top achievements
Rank 2
answered on 01 Apr 2014, 04:32 AM
Hi,

If you want to set the selected value, you have to use the ItemDataBound event.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && !e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
  {
    GridEditableItem edit = (GridEditableItem)e.Item;
    DropDownList drop = (DropDownList)edit.FindControl("DropDownList1");
    drop.DataSourceID = "SqlDataSource1";
    drop.DataTextField = "SameRegion";
    drop.DataValueField = "SameRegion";
   //Set the selected value in edit
    drop.SelectedValue = DataBinder.Eval(edit.DataItem, "SameRegion").ToString();
  }
}

Thanks,
Shinu
0
RB
Top achievements
Rank 1
answered on 01 Apr 2014, 11:04 PM
Thanks for the reply. If "SameRegion" allows null, I want the dropdown to diaplay a blank field also.
droplist.DataSource = this._AvrmQueueTypeTable;
droplist.DataTextField = DBColName[1];
droplist.DataValueField = DBColName[1];                       
droplist.DataBind();
droplist.Items.Insert(0, new ListItem(String.Empty, String.Empty));  

It didnt work.
Also droplist.Items.Insert(0, new ListItem{ Text = "", Value = "" }); did not work!
The user should be able to select a blank entry from the dropdown since it allows null values.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 02 Apr 2014, 04:20 AM
Hi,

Please add the following code snippet in the ItemCreated event of the RadGrid.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && !e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode)
  {
    GridEditableItem edit = (GridEditableItem)e.Item;
   //Your code to add dropdown
    drop.AppendDataBoundItems = true;
    drop.Items.Insert(0, new ListItem(String.Empty, String.Empty));
    edit["SameRegion"].Controls.Add(drop);
  }
}

Thanks,
Shinu
0
RB
Top achievements
Rank 1
answered on 02 Apr 2014, 04:09 PM
Thanks. I was missing  drop.AppendDataBoundItems = true !!
Tags
Grid
Asked by
RB
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
RB
Top achievements
Rank 1
Share this question
or