ComboBoxColumn

4 posts, 2 answers
  1. Morghabi
    Morghabi avatar
    2 posts
    Member since:
    Jun 2016

    Posted 20 Jun Link to this post

    I need a ComboBox column initialized with some data and the user can enter new data too when inserting new row into grid. For example, ComboBox column has three items by default Phone, Mobile, Fax, but the user needs to insert new item (e.g., Code). How can I do this?

    Here is the code. With this code the user can only select one of the default values of combo column and he/she cannot enter his/her new value into combo column.

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Value", typeof(string));
    var details = db.BatchItemDetails.Where(d => d.BatchItem_Id == _id)
                           .Select(d => new { d.Name, d.Value });
    foreach (var row in details)
       table.Rows.Add(row.Name, row.Value);
     
    grid.MasterTemplate.AutoGenerateColumns = false;
    grid.DataSource = table;
     
    GridViewComboBoxColumn name = new GridViewComboBoxColumn();
    name.DataSource = (from b in db.Batches
                 join i in db.BatchItems on b.Id equals i.Batch_Id
                 join d in db.BatchItemDetails on i.Id equals d.BatchItem_Id
                 where b.Id == batchId
                 orderby d.Name
                 select d.Name).Distinct().ToArray(); // string array.
     
    name.AutoCompleteMode = AutoCompleteMode.Suggest;
    name.DropDownStyle = RadDropDownStyle.DropDown;
    name.Name = "Name";
    name.FieldName = "Name";
    name.HeaderText = "عنوان اطلاعات";
    name.Width = 219;
    name.TextAlignment = ContentAlignment.MiddleLeft;
    grid.Columns.Add(name);
     
    grid.Columns.Add("Value", "شرح اطلاعات", "Value");
    grid.Columns["Value"].Width = 353;
     
    grid.AutoSizeRows = true;

  2. Answer
    Hristo
    Admin
    Hristo avatar
    714 posts

    Posted 20 Jun Link to this post

    Hello Morghabi,

    Thank you for writing.

    We have an example in our documentation handling exactly this type of scenario: Allow end-users to add items to DropDownListEditor.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Morghabi
    Morghabi avatar
    2 posts
    Member since:
    Jun 2016

    Posted 21 Jun in reply to Hristo Link to this post

    The article is so complex for me that am a beginner in C#. Please clarify the article for me. Thanks.
  5. Answer
    Hristo
    Admin
    Hristo avatar
    714 posts

    Posted 21 Jun Link to this post

    Hi Morghabi,

    Thank you for writing back.

    The example discusses a scenario for populating items to a bound drop-down list editor. The tricky part in this setup is to extract the newly entered value and update the  data source of the editor. To do so we need to perform a check if the element is already added and in that case call the base implementation.

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top