How to add a blank or empty row?

6 posts, 0 answers
  1. Ryan
    Ryan avatar
    13 posts
    Member since:
    May 2010

    Posted 15 May 2012 Link to this post

    Hello,

    We're currently using a radmulticolumncombobox populated through a dataset, and need the capability of selecting a blank row. Within our database the field it refers to is non-null, and the client has to be able to select nothing for a value.

    I've tried using lstParentGroup.EditorControl.Rows.AddNew(), but this adds the row to the very bottom of the collection. We definitely want the blank row at the top of the list. Also, when this row is added, the control seems to get confused when using lstParentGroup.SelectedValue functionality. What happens is the blank row at the bottom is selected instead of what should be selected.

    Anything I've missed in the designer, or possible solutions to this problem?

    Thank you very much for your time!
    Ryan
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 18 May 2012 Link to this post

    Hi Ryan,

    Thank you for writing.

    You can add dummy row in your DataSource and pin the related GridViewRowInfo instance to the top in order to achieve your scenario. Here is a sample application with DataTable binding and the event handler implementation:
    using System;
    using System.Data;
    using Telerik.WinControls.UI;
     
    namespace Lab.Combo
    {
        public partial class MultiComboEmptyRow : MainForm
        {
            private RadMultiColumnComboBox comboBox = new RadMultiColumnComboBox();
            private DataRow emptyRow;
            public event EventHandler NoneSelected;
     
            public MultiComboEmptyRow()
            {
                InitializeComponent();
     
                comboBox.Parent = this;
                comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;
     
            }
     
            void comboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridViewRowInfo current = this.comboBox.EditorControl.CurrentRow;
                if (current != null)
                {
                    DataRow row = (current.DataBoundItem as DataRowView).Row;
                    if (row == emptyRow && this.NoneSelected != null)
                    {
                        this.NoneSelected(this, new EventArgs());
                    }
                }
            }
     
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
     
                DataTable table1 = new DataTable();
                table1.Columns.Add("ID");
                table1.Columns.Add("Name");
     
                this.emptyRow = table1.Rows.Add(null, null);
     
                table1.Rows.Add(1, "Ivan Petrov");
                table1.Rows.Add(2, "Stefan Muler");
                table1.Rows.Add(3, "Alexandro Ricco");
     
                this.comboBox.DataSource = table1;
                comboBox.DisplayMember = "Name";
     
                SetTopEmptyRow();
            }
     
            private void SetTopEmptyRow()
            {
                foreach (GridViewRowInfo row in this.comboBox.EditorControl.Rows)
                {
                    if ((row.DataBoundItem as DataRowView).Row == this.emptyRow)
                    {
                        row.PinPosition = PinnedRowPosition.Top;
                        break;
                    }
                }
            }
        }
    }

    I hope this helps.

    Regards,
    Julian Benkov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ryan
    Ryan avatar
    13 posts
    Member since:
    May 2010

    Posted 28 May 2012 Link to this post

    Hi Julian,

    Thanks for your reply so far.

    The main problem is that my combobox is already databound through a table adapter. I can create the new row and pin it to the top, but when it comes time to fill the table adapter again, Object Reference exceptions are raised because of the blank row. I can remove the row before filling then add it again, but this leads to a unique problem where two blank rows exist.

    Thanks for your help
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 30 May 2012 Link to this post

    Hello Ryan,

    In this situation, you should remove the row from the PinPosition:

    this.gridView.Rows[0].PinPosition = PinnedRowPosition.None;

    After that remove the row from the DataSource object and fill the DataSouce. Then you can add the empty row again and pin to top position.

    I hope this helps.

    Kind regards,
    Julian Benkov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  6. Ryan
    Ryan avatar
    13 posts
    Member since:
    May 2010

    Posted 31 May 2012 Link to this post

    Here is the final solution for me:

    Dim emptyRow As Telerik.WinControls.UI.GridViewRowInfo
     
                        If emptyRow IsNot Nothing Then
                            emptyRow.PinPosition = PinnedRowPosition.None
                        End If
                        Dim rows As New List(Of GridViewRowInfo)(Me.lstParentGroup.EditorControl.Rows)
                        If rows.Any Then
                            For Each row As Telerik.WinControls.UI.GridViewDataRowInfo In rows
                                row.Delete()
                            Next
                        End If
                        GroupByJobTableAdapter.FillByJobID(DataMain.GroupByJob, lstJob.SelectedValue)
                        emptyRow = lstParentGroup.EditorControl.Rows.AddNew()
                        emptyRow.PinPosition = PinnedRowPosition.Top
                        emptyRow.Cells("GroupNo").Value = "None"

    Thank you very much for you help!

    Cheers,
    Ryan
  7. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 05 Jun 2012 Link to this post

    Hello Ryan,

    I am glad to hear that you have solved your issue. 

    Feel free to ask if you have any additional questions. 

    Greetings,
    Julian Benkov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready