PROBLEM add item to dropdown list editor of GridViewMultiComboBoxColumn

1 Answer 92 Views
GridView
Eric
Top achievements
Rank 1
Eric asked on 11 Oct 2021, 05:40 AM

Hello , i want to see if GridViewMultiComboBox is suitable for our application and i want the end user to type any text gridview multicombo box column and i checked  https://www.telerik.com/forums/editable-values-for-new-row-using-gridviewmulticomboboxcolumn but it is giving me problem after end user type and add new item to dropdown list editor , when i go to different row and try to change selected value og MultiComboBox , it doesnt fire selectedvalue changed ..

Also when you change the selected value of GridviewMultiComboBox column it should automatically get the price of the item , update gridview item price column , and multiply with quantity column and update total column ,,,it does that but after i added new item manually , it doesnt do that ..Code is below ..If you see the pictures , after added dropdown list item , changing the item from the list doesnt effect the price and item number and total column anymore

 

                                                                    

        private void ERPDataForm_Load(object sender, EventArgs e)
        {

                this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
                this.radGridView1.EnableFiltering = false;
                this.radGridView1.MasterTemplate.ShowHeaderCellButtons = false;
                this.radGridView1.MasterTemplate.ShowFilteringRow = false;
                this.radGridView1.MasterTemplate.EnableGrouping = false;
                this.radGridView1.MasterTemplate.AllowAddNewRow = true;
                this.radGridView1.AddNewRowPosition = SystemRowPosition.Bottom;      
                this.radGridView1.AllowAutoSizeColumns = true;
                this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

                GridViewDecimalColumn qtyColumn = new GridViewDecimalColumn();
                qtyColumn.Name = "QtyColumn";
                qtyColumn.HeaderText = "Qty";
                qtyColumn.FieldName = "Quantity";
                qtyColumn.DecimalPlaces = 0;
                qtyColumn.TextAlignment = ContentAlignment.MiddleCenter;
                qtyColumn.Width = 80;
                radGridView1.MasterTemplate.Columns.Add(qtyColumn);

                GridViewDecimalColumn itemIDColumn = new GridViewDecimalColumn();
                itemIDColumn.Name = "itemIDColumn";
                itemIDColumn.HeaderText = "Item ID";
                itemIDColumn.FieldName = "ItemID";
                itemIDColumn.DecimalPlaces = 0;
                itemIDColumn.TextAlignment = ContentAlignment.MiddleCenter;
                itemIDColumn.Width = 100;
                radGridView1.MasterTemplate.Columns.Add(itemIDColumn);

       
                DataTable gg = new DataTable();
                gg = CreateDataTable2();
                GridViewMultiComboBoxColumn descriptionColumn = new GridViewMultiComboBoxColumn();
                descriptionColumn.DataSource = gg;
                descriptionColumn.Name = "DecriptionColumn";
                descriptionColumn.HeaderText = "Description";
                descriptionColumn.DisplayMember = "DescX";
                descriptionColumn.FieldName = "ItemID";

                descriptionColumn.ValueMember = "ItemIDX";
                descriptionColumn.DropDownStyle = RadDropDownStyle.DropDown;
                descriptionColumn.AutoCompleteMode = AutoCompleteMode.Suggest;
                descriptionColumn.Width = 500;
                descriptionColumn.AutoSizeMode = BestFitColumnMode.AllCells;          


                radGridView1.MasterTemplate.Columns.Add(descriptionColumn);

                GridViewDecimalColumn itemPriceColumn = new GridViewDecimalColumn();
                itemPriceColumn.Name = "itemPriceColumn";
                itemPriceColumn.HeaderText = "Unit Price";
                itemPriceColumn.FieldName = "ItemPrice";
                itemPriceColumn.DecimalPlaces = 2;
                itemPriceColumn.TextAlignment = ContentAlignment.MiddleRight;
                itemPriceColumn.Width = 120;
                itemPriceColumn.FormatString = "${0:###,###0.00}";

                radGridView1.MasterTemplate.Columns.Add(itemPriceColumn);

                GridViewDecimalColumn itemTotalColumn = new GridViewDecimalColumn("Total Amount");
                itemTotalColumn.Name = "itemTotalColumn";
                itemTotalColumn.HeaderText = "TOTAL";
                itemTotalColumn.FieldName = "ItemTotal";
                itemTotalColumn.Width = 110;
                itemTotalColumn.Expression = "itemPriceColumn * QtyColumn";
                itemTotalColumn.FormatString = "${0:###,###0.00}";
                radGridView1.MasterTemplate.Columns.Add(itemTotalColumn);

                BusinessLayerInvoiceItems BLII = new BusinessLayerInvoiceItems();

                ArrayList al = new ArrayList();
                al = BLII.GetInvoiceItemsbyInvoiceID(INVOICEID);          

                this.radGridView1.DataSource = al;


                this.radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
               this.radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);

               this.radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);       

}

  private DataTable CreateDataTable2()
  {

            DataTable dtable2 = new DataTable();
            //set columns names
            dtable2.Columns.Add("ItemIDX", typeof(System.Int32));
            dtable2.Columns.Add("Descx", typeof(System.String));            
            dtable2.Columns.Add("ItemPrice", typeof(System.String));
            dtable2.Columns.Add("ItemQuantity", typeof(System.String));


            BusinessLayerItem BLC = new BusinessLayerItem();
            ArrayList al = new ArrayList();
            Item c = new Item();


            al = BLC.GetItems();

            DataRow drowX = dtable2.NewRow();

            drowX["ItemIDX"] = -1;
            drowX["DescX"] = "";            
            drowX["ItemPrice"] = "";
            drowX["ItemQuantity"] = "";
            dtable2.Rows.Add(drowX);


            for (int i = 0; i < al.Count; i++)
            {



                //Add Rows
                DataRow drow = dtable2.NewRow();

                drow["ItemIDX"] = ((Item)al[i]).ItemId;
                drow["DescX"] = ((Item)al[i]).ItemName.ToUpper();                
                drow["ItemPrice"] = ((Item)al[i]).ItemPrice;
                drow["ItemQuantity"] = ((Item)al[i]).ItemQuantity;
                dtable2.Rows.Add(drow);


            }
            return dtable2;
        }

 

        protected void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (e.EditorType == typeof(RadMultiColumnComboBoxElement) )
            {
                if (!isrowadded)
                {
                    e.EditorType = typeof(CustomRadMultiColumnComboBoxElement);                    
                    isrowadded = false;
                }
                else
                {
                    e.EditorType = typeof(RadMultiColumnComboBoxElement);
                }

            }
        }

        protected void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            RadMultiColumnComboBoxElement mccbEditor = e.ActiveEditor as RadMultiColumnComboBoxElement;

            if (mccbEditor != null)
            {
                FilterDescriptor descriptor = new FilterDescriptor("DescX", FilterOperator.StartsWith, null);
                mccbEditor.EditorControl.FilterDescriptors.Add(descriptor);              
                mccbEditor.AutoSizeDropDownToBestFit = true;
            }            

        }

        bool isColumnAdded;
        void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            if (this.radGridView1.CurrentColumn is GridViewMultiComboBoxColumn)
            {
                if (!isColumnAdded)
                {
                    isColumnAdded = true;
                    RadMultiColumnComboBoxElement editor = (RadMultiColumnComboBoxElement)this.radGridView1.ActiveEditor;
                    editor.EditorControl.MasterTemplate.AutoGenerateColumns = false;
                    editor.EditorControl.Columns.Add(new GridViewTextBoxColumn("ItemIDX"));
                    editor.EditorControl.Columns.Add(new GridViewTextBoxColumn("DescX"));
                    editor.EditorControl.Columns.Add(new GridViewTextBoxColumn("ItemPrice"));
                    editor.EditorControl.Columns.Add(new GridViewTextBoxColumn("ItemQuantity"));
                    editor.AutoSizeDropDownToBestFit = true;           


                    editor.SelectedIndexChanged += new EventHandler(ProcCode_multiColumnComboElement_SelectedIndexChanged);

                }
            }
        }


        void ProcCode_multiColumnComboElement_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (radGridView1.ActiveEditor is RadMultiColumnComboBoxElement)
            {
                RadMultiColumnComboBoxElement multiColumnComboElement = (RadMultiColumnComboBoxElement)radGridView1.ActiveEditor;

                    GridViewRowInfo row = multiColumnComboElement.EditorControl.CurrentRow;
                    if (row != null)
                    {
                        radGridView1.CurrentRow.Cells[1].Value = row.Cells[0].Value;
                        radGridView1.CurrentRow.Cells[3].Value = row.Cells[2].Value;                 
                    }

             }                         

   
        }

        bool isrowadded = false;

        private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
        {     
            GridViewRowInfo rowX = e.Row;
            rowX.Tag = "new row";
            isrowadded = true;
            this.radGridView1.CurrentRow = this.radGridView1.Rows[this.radGridView1.Rows.Count - 1];

        }     

Custom CLASS Below

public class CustomRadMultiColumnComboBoxElement : RadMultiColumnComboBoxElement

    {


        public CustomRadMultiColumnComboBoxElement()

        {

            this.AutoFilter = true;

        }

        protected override Type ThemeEffectiveType

        {

            get

            {

                return typeof(RadMultiColumnComboBoxElement);

            }

        }


   


        public override void ProcessReturnKey(System.Windows.Forms.KeyEventArgs e)

        {

            try

            {

                string text = this.Text;


                if (!string.IsNullOrEmpty(text))

                {


                    GridViewRowInfo newCurrentRow = this.FindItemExact(text) as GridViewRowInfo;


                    if (newCurrentRow == null)

                    {

                        this.AddTextAsRow(text);

                    }

                }


                base.ProcessReturnKey(e);

            }

            catch (Exception ex)

            {

                base.ProcessReturnKey(e);

            }

        }


        public override object Value

        {

            get

            {

                this.AddTextAsRow(this.Text);

                return base.Value;

            }

            set

            {

                base.Value = value;

            }

        }


        private int id = 0;

        private void AddTextAsRow(string text)

        {

            for (int i = 0; i < this.EditorControl.Rows.Count; i++)

            {


                GridViewRowInfo newCurrentRow = this.EditorControl.Rows[i] as GridViewRowInfo;

                string trr = newCurrentRow.Cells[1].Value.ToString();

                if (trr == Text.Trim() || Text.Trim()=="")

                {

                    return;

                }

            }


            

            GridViewDataRowInfo row = (GridViewDataRowInfo)this.EditorControl.Rows.AddNew();

            row.Cells["ItemIDX"].Value = id;

            row.Cells["DescX"].Value = text;

            row.Cells["ItemPrice"].Value = id;

            row.Cells["ItemQuantity"].Value = id;



           // this.EditorControl.Rows.Add(id, text);

            

            id++;

        }

    }

                                                                                  
Eric
Top achievements
Rank 1
commented on 11 Oct 2021, 08:04 PM

any help here ? we want to be familiar with Telerik tools but it gets more complicated when we try to adjust them .

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Oct 2021, 02:26 PM
Hello, Eric,

I would like to note that the GridViewMultiComboBoxColumn accepts only valid values according to the applied DataSource to the column. If you enter any custom input (that doesn't match any record in the drop-down items), it wouldn't be considered as valid. Hence, it won't be stored in the cell and this is the expected behavior. If you need to allow the end-users to add custom values, it would be necessary this new value to be added to the DataSource collection that is applied to the column.

I have reviewed the provided sample code snippet and prepared a project to test the behavior on my end. I have noticed that the suggested solution in the referred forum thread is not applied in the code snippet since the custom implementation for RadMultiColumnComboBoxElement doesn't add the custom text to the applied column's DataSource collection. I have updated this part following the approach form the forum thread. Please give the sample project a try and see how it works on your end. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.

Off topic, please have in mind that threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Tags
GridView
Asked by
Eric
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or