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

BeginEdit

17 Answers 447 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Deborah
Top achievements
Rank 1
Deborah asked on 05 Nov 2010, 10:16 PM
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!

17 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Nov 2010, 10:51 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 10:57 PM
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
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Nov 2010, 11:00 PM
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

0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 11:06 PM
Hi Emanuel,

It's true, but I put them in to replicate what Deborah had done.
Thanks
Richard
0
Deborah
Top achievements
Rank 1
answered on 05 Nov 2010, 11:18 PM
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?
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Nov 2010, 11:21 PM
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

0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 11:43 PM
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
0
Deborah
Top achievements
Rank 1
answered on 05 Nov 2010, 11:46 PM
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 -
0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 11:49 PM
Hi Deborah,

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

Richard
0
Deborah
Top achievements
Rank 1
answered on 05 Nov 2010, 11:54 PM
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!
0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 11:58 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 06 Nov 2010, 12:02 AM
As a side note, you may be interested in this article which mentions BeginUpdate() and EndUpdate() in regard to performance.
Richard
0
Deborah
Top achievements
Rank 1
answered on 06 Nov 2010, 12:07 AM
Thanks for the tips.
0
Jared
Top achievements
Rank 2
answered on 02 Jun 2016, 09:31 PM

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();
    }
}

0
Hristo
Telerik team
answered on 07 Jun 2016, 01:14 PM
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.
0
Hengky
Top achievements
Rank 1
Veteran
answered on 12 Jan 2018, 01:49 AM

Hello all... 

How to make this happen when i called it from another form 

        Dim currentCell As GridViewCellInfo = RadGridView1.Rows(RadGridView1.Rows.Count - 1).Cells(1)

        currentCell.IsSelected = True
        currentCell.BeginEdit()

 

This the code i wrote for fill raggridview form another form, i need to edit mode cell 2.

Dim frm = Application.OpenForms("FrmBeli")
                        If frm IsNot Nothing Then
                            If GridCari.CurrentRow.Index <> -1 Then
                                i = GridCari.CurrentRow.Index
                                CType(frm, FrmBeli).GridDetail.ClearSelection()
                                CType(frm, FrmBeli).GridDetail.Rows.Add(GridCari.Rows(i).Cells(0).Value.ToString(), GridCari.Rows(i).Cells(1).Value.ToString())
                                CType(frm, FrmBeli).GridDetail.BeginEdit()
                            End If
                        End If

 

Thanks before

0
Hristo
Telerik team
answered on 12 Jan 2018, 02:28 PM
Hello Hengky,

Thank you for writing.

As long as you can access the form with the grid from the other form you should not have issues with setting a particular cell in EditMode. For example, you can expose a public method in the grid form and pass as parameters the row and column indices of the cell you want to select. Then you can call the RadGridView.BeginEdit method.

In case you need further assistance please provide me with more details about your actual scenario.

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

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Deborah
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Deborah
Top achievements
Rank 1
Jared
Top achievements
Rank 2
Hristo
Telerik team
Hengky
Top achievements
Rank 1
Veteran
Share this question
or