BeginEdit

16 posts, 2 answers
  1. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 05 Nov 2010 Link to this post

    I read many of the posts here on how to properly set BeginEdit to programmatically set a cell into Edit mode. However, the code is not working for me.

    Here is the code I am using:
    Dim currentCell = RemindersGridView.Rows(RemindersGridView.Rows.Count - 1).Cells(2)
    currentCell.RowInfo.IsCurrent = True
    currentCell.ColumnInfo.IsCurrent = True
    currentCell.BeginEdit()

    It does not work under these circumstances:
    1) The second time the code is called.
    I have the code in an Add button, so when the user clicks Add to add a new row, the 3rd cell is automatically opened for edit. This works the first time I click the Add button. But if I press it a second time, it does not work. I tried to call EndEdit before this code, and that did not help.

    2) If the cell is a drop down list.
    If the cell to automatically open for edit is a drop down list, the code does not work. It does not show the comboBox editor.

    Any tips for fixing either of these issues is appreciated!

    thanks!
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Nov 2010 Link to this post

    Hello Deborah,

    Please try something like this: (if you have any problems please uncomment those lines, the first one should be in the case of multiselection to prevent multiple cells from being selected, and the second one is because the grids virtualization mechanism to force all pending async operations to be performed)
    //radGridView1.ClearSelection();
    var currentCell = radGridView1.Rows[radGridView1.Rows.Count - 1].Cells[2];
    currentCell.IsSelected = true;
    currentCell.BeginEdit();
    //radGridView1.RootElement.PerformLayout();
    var dropDownEditor = radGridView1.ActiveEditor as RadDropDownListEditor;
    if (dropDownEditor != null)
    {
        var element = dropDownEditor.EditorElement as RadDropDownListEditorElement;
        element.ShowPopup();
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Nov 2010 Link to this post

    Hi Deborah and Emanuel,

    Looks like Emanuel has addressed the drop down here. I've just tried this. Almost the same as your code...
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        Dim currentCell As GridViewCellInfo = Me.RadGridView.Rows(2).Cells(1)
        currentCell.ColumnInfo.IsCurrent = True
        currentCell.RowInfo.IsCurrent = True
        currentCell.IsSelected = True
        currentCell.RowInfo.IsSelected = True
        currentCell.BeginEdit()
    End Sub

    and from the button click it always works the second time around too. Perhaps in your case the endEdit is not being called correctly. You can always call EndEdit() before begin edit to see if that's the case.

    Hope that helps also.
    Richard..


    //Off Topic - Emanuel - congratulations on your MVP status. Totally deserved. - Richard
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Nov 2010 Link to this post

    Hello Richard,

    You don't need these lines:
    currentCell.ColumnInfo.IsCurrent = True
    currentCell.RowInfo.IsCurrent = True
    currentCell.RowInfo.IsSelected = True

    What I've posted earlier is working by itself, because you when you say BeginEdit() you are starting an edit operation on the CurrentCell, not on the selected row, and not on the selected column....


    Best Regards,
    Emanuel Varga

  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Nov 2010 Link to this post

    Hi Emanuel,

    It's true, but I put them in to replicate what Deborah had done.
    Thanks
    Richard
  7. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 05 Nov 2010 Link to this post

    Thanks for your reply. But it does not do anything different. It still does not work. Here is my updated code:

    RemindersGridView.ClearSelection()
    Dim currentCell = RemindersGridView.Rows(RemindersGridView.Rows.Count - 1).Cells(2)
    currentCell.IsSelected = True
    currentCell.BeginEdit()
    RemindersGridView.RootElement.PerformLayout()
    Dim dropDownEditor = DirectCast(RemindersGridView.ActiveEditor, RadDropDownListEditor)
    If dropDownEditor IsNot Nothing Then
        Dim element = DirectCast(dropDownEditor.EditorElement, RadDropDownListEditorElement)
        element.ShowPopup()
    End If

    When I break on the If statement, the ActiveEditor is nothing. I am also NOT getting a CellBeginEdit event after executing the BeginEdit statement.

    As I mentioned earlier, this is in an Add button click event. Is it possible that yours works and mine does not because I am adding the row to the underlying datasource in the same method?

    Here is the entire click event:
    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        ' Create the new reminder item
        Dim ReminderListItem As Reminder = Reminder.Create
        ' Add the new item to the bound list
        Me.ReminderList.Add(ReminderListItem)
        ' Select the added row and the text cell
        RemindersGridView.ClearSelection()
        Dim currentCell = RemindersGridView.Rows(RemindersGridView.Rows.Count - 1).Cells(2)
        currentCell.IsSelected = True
        currentCell.BeginEdit()
        RemindersGridView.RootElement.PerformLayout()
        Dim dropDownEditor = DirectCast(RemindersGridView.ActiveEditor, RadDropDownListEditor)
        If dropDownEditor IsNot Nothing Then
            Dim element = DirectCast(dropDownEditor.EditorElement, RadDropDownListEditorElement)
            element.ShowPopup()
        End If
    End Sub


    Any other ideas?
  8. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Nov 2010 Link to this post

    Hello again,

    The code i've posted earlier is fired from a button click, please take a look at this example and tell me if this is working for you, and then we'll continue from there:
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
     
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(radGridView1 = new RadGridView());
            this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
     
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.MultiSelect = true;
     
            var button = new RadButton();
            button.Text = "Click me";
            button.Dock = DockStyle.Bottom;
            button.Click += new EventHandler(button_Click);
            this.Controls.Add(button);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.DataSource = new ProductsCollection(1000);
        }
     
        void button_Click(object sender, EventArgs e)
        {
            //radGridView1.ClearSelection();
            var currentCell = radGridView1.Rows[radGridView1.Rows.Count - 1].Cells[2];
            currentCell.IsSelected = true;
            currentCell.BeginEdit();
            //radGridView1.RootElement.PerformLayout();
            var dropDownEditor = radGridView1.ActiveEditor as RadDropDownListEditor;
            if (dropDownEditor != null)
            {
                var element = dropDownEditor.EditorElement as RadDropDownListEditorElement;
                element.ShowPopup();
            }
        }
     
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            radGridView1.Columns["BuyerId"].IsVisible = false;
     
            var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
            column.DataSource = new BuyersCollection(10, 10);
     
            column.ValueMember = "Id";
            column.FieldName = "BuyerId";
     
            column.DisplayMember = "Name";
            this.radGridView1.Columns.Add(column);
     
            this.radGridView1.BestFitColumns();
        }
    }
     
    #region Helpers
     
    public class Product : INotifyPropertyChanged
    {
        private int id, buyerId;
     
        public int BuyerId
        {
            get { return buyerId; }
            set
            {
                buyerId = value;
                OnPropertyChanged("BuyerId");
            }
        }
     
        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                OnPropertyChanged("Id");
            }
        }
     
        public Product(int id, int buyerId)
        {
            this.Id = id;
            this.BuyerId = buyerId;
        }
     
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     
        public event PropertyChangedEventHandler PropertyChanged;
    }
     
    public class Buyer
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public Buyer(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
     
    public class ProductsCollection : BindingList<Product>
    {
        public ProductsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Product(i, i + 10));
            }
        }
    }
     
    public class BuyersCollection : BindingList<Buyer>
    {
        public BuyersCollection(int startIndex, int lenght)
        {
            for (int i = 0; i < 10; i++)
            {
                this.Add(new Buyer(i + 10, "Buyer" + (i + 1)));
            }
        }
    }
     
    #endregion Helpers

  9. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Nov 2010 Link to this post

    Hi Deborah,

    I think that the issue comes from trying to add a new row and also begin edit at the same time. I was able to replicate your issue of not being able to edit on the second time. I managed to solve this by calling BeginUpdate and endUpdate on the RadGridView.
    See the example below.

    Imports Telerik.WinControls.UI
      
    Public Class Form1
      
        Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
      
            RadGridView1.BeginUpdate()
            Dim rowInfo As GridViewRowInfo = Me.RadGridView1.Rows.AddNew()
            rowInfo.Cells(0).Value = "New1"
            rowInfo.Cells(1).Value = "New2"
            RadGridView1.ClearSelection()
            RadGridView1.EndUpdate()
      
      
            Dim currentCell As GridViewCellInfo = RadGridView1.Rows(RadGridView1.Rows.Count - 1).Cells(1)
            currentCell.IsSelected = True
            currentCell.BeginEdit()
        End Sub
      
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("A"))
            Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("B"))
            Dim rowInfo As GridViewRowInfo = Me.RadGridView1.Rows.AddNew()
            rowInfo.Cells(0).Value = "A1"
            rowInfo.Cells(1).Value = "B1"
            rowInfo = Me.RadGridView1.Rows.AddNew()
            rowInfo.Cells("A").Value = "A2"
            rowInfo.Cells("B").Value = "B2"
      
        End Sub
    End Class

    Taking away the lines..
    RadGridView1.BeginUpdate()
    ...
    RadGridView1.EndUpdate()
    shows that you can no longer perform the operation twice.

    Let me know if that helps
    Richard
  10. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 05 Nov 2010 Link to this post

    Thank you for your post.

    Note from my original post that the code I posted *does* work when I click the Add button if the column is a Textbox AND it is the first time that I click. It just does not work a second time. Nor does it work with a combobox. NOTE: I am using a Combox and not a DropDownList because DropDownlist is not a choice in the designer and this grid was created with the designer. Could that be the problem?

    I wondered if I need to somehow end the prior edit?
    Or if I need to use a different event because the new row was not finished generating when I just added it to the underlying list?

    Thanks -
  11. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Nov 2010 Link to this post

    Hi Deborah,

    Have you tried the BeginUpdate() and EndUpdate() as I suggested?

    Richard
  12. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 05 Nov 2010 Link to this post

    SWEET!

    Thank you! Thank you! Thank you!

    I posted my prior message before I received your reply. Using BeginUpdate and EndUpdate worked. I had tried it before, but not around the code I was using to add the row.

    For anyone else with this problem note that this ALSO fixed the problem with the combobox.

    Just one more question if I may ... what is BeginUpdate and EndUpdate doing exactly?

    Thanks again!
  13. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Nov 2010 Link to this post

    Hi Deborah,

    Glad that this has worked for you. Please remember to mark as answer so others can find the solution too.
    BeginUpdate prepares the grid for certain operations to take place and is also used to enhance performance.

    Best regards,
    Richard
  14. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Nov 2010 Link to this post

    As a side note, you may be interested in this article which mentions BeginUpdate() and EndUpdate() in regard to performance.
    Richard
  15. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 05 Nov 2010 Link to this post

    Thanks for the tips.
  16. Jared
    Jared avatar
    6 posts
    Member since:
    Oct 2015

    Posted 02 Jun Link to this post

    This seems to be one of the few WinForms threads on the topic of activating the drop down by just clicking once into a cell.  I've modified the accepted answer a bit to fire during the cell's click event.  The accepted answer works but actually then causes all other columns to activate the edit mode upon a cell's click.  I also shortened the lines of code up using C#'s new null propagation operator.

    private void Dg_CellClick(object sender, GridViewCellEventArgs e)
    {
        if (dg.Columns[e.ColumnIndex] is GridViewComboBoxColumn)
        {
            var currentCell = dg.Rows[e.RowIndex].Cells[e.ColumnIndex];
            currentCell.BeginEdit();
     
            var dropDownEditor = dgSamples.ActiveEditor as RadDropDownListEditor;
            var editorElement = dropDownEditor?.EditorElement as RadDropDownListEditorElement;
            editorElement?.ShowPopup();
        }
    }

  17. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    710 posts

    Posted 07 Jun Link to this post

    Hello Jared,

    Thank you for sharing your solution.

    Indeed, this is a valid approach for opening up the drop down.

    Please let me know if you need additional 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
UI for WinForms is Visual Studio 2017 Ready