How to set focus to first column in add line

10 posts, 0 answers
  1. Ruth Goldberg
    Ruth Goldberg avatar
    54 posts
    Member since:
    Dec 2009

    Posted 01 Mar 2010 Link to this post

    When on an add line, how can I set the focus to the first column? The column that gets the focus is wherever the user clicks on the line. I tried to set the focus in CurrentRowChanged event with this line:

     

        CType(e.CurrentRow, GridViewNewRowInfo).Cells("Acct_Code").CellElement.Focus()

     

     

    but it has no effect. I looked into some of the other events, RowChanging and DefaultValuesNeeded, but CellElement is nothing at that point.
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 02 Mar 2010 Link to this post

    Hi Ruth Goldberg,

    I am not sure that I completely understand your question. You can change the current column by settingGridViewColumn.IsCurrent property. For example:

    Me.radGridView1.Columns("Acct_Code").IsCurrent = True

    Then you can start editing a cell by calling RadGridView.BeginEdit method. If this doesn't help, please describe in more detail the exact behavior that you want to achieve. I will be glad to help further.

    Regards,

    Jack
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Ruth Goldberg
    Ruth Goldberg avatar
    54 posts
    Member since:
    Dec 2009

    Posted 04 Mar 2010 Link to this post

    When I set the cell as current and begin edit as I show in the following code, the field is highlighted and ready to edit, but the cursor is not inside the cell. You have to click in it to get the focus there. What else can I do so the user doesn't have to click but can just start typing?
                CType(e.CurrentRow, GridViewNewRowInfo).Cells("Acct_Code").CellElement.Enabled = True 
                Me.TblAccountRadGridView.Columns("Acct_Code").IsCurrent = True 
                Me.TblAccountRadGridView.BeginEdit()  
      
  4. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 05 Mar 2010 Link to this post

    Hello Ruth Goldberg,

    Thank you for writing back. This code should do the work, however it should be called when RadGridView is visible. You can call it in OnShown method of your form. Consider the code snippet below:

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        this.radGridView1.BeginEdit();
    }

    Please send me your project if this doesn't help. I will try to find a working solution.

    Greetings,

    Jack
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Ruth Goldberg
    Ruth Goldberg avatar
    54 posts
    Member since:
    Dec 2009

    Posted 08 Mar 2010 Link to this post

    I don't understand your answer. The Shown event (I'm writing in vb not C#, as you can see) is called for the form. I want to start the edit and have the cursor in the first column when I click in the Add line, not when I open the form. Please explain what your code is supposed to accomplish. It would be more helpful if you used vb instead of C#.
  6. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 09 Mar 2010 Link to this post

    Hello Ruth Goldberg,

    Thank you for this clarification. If I understand your question correctly, you want to make the first column current and editable when clicking on "add new row". Here is the code in VB:

    Me.RadGridView1.GridBehavior = New MyGridBehavior()
     
    Public Class MyGridBehavior
        Inherits BaseGridBehavior
     
        Public Overrides Function OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) As Boolean
            Dim element As GridRowElement = TryCast(GridControl.ElementTree.GetElementAtPoint(e.Location), GridRowElement)
            If element IsNot Nothing Then
            GridControl.Columns(0).IsCurrent = True
            GridControl.MasterGridViewInfo.TableAddNewRow.IsCurrent = True
            GridControl.BeginEdit()
            Return True
            End If
            Return MyBase.OnMouseDown(e)
        End Function
    End Class

    I hope this helps.

    Kind regards,

    Jack
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Andy F.
    Andy F. avatar
    46 posts
    Member since:
    May 2006

    Posted 19 Dec 2017 in reply to Jack Link to this post

    This does not work when another row is currently being edited.  Steps:

    1. Click to edit a cell in another row, such that it shows a textbox for editing.

    2. Click the Add New row.

    What happens is that the same column that was being edited is the column that will start the New Row edits, not the first column.  Setting IsCurrent appears to be getting ignored.

  8. Hristo
    Admin
    Hristo avatar
    1126 posts

    Posted 20 Dec 2017 Link to this post

    Hello Andy,

    Thank you for writing.

    The code snippet my colleague has posted will no longer compile as the grid does not have a MasterGridViewInfo property. I am posting an updated version below: 
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
     
            this.radGridView1.GridBehavior = new MyGridBehavior();
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i), i % 2 == 0);
            }
     
            return dt;
        }
    }
     
    public class MyGridBehavior : BaseGridBehavior
    {
        public override bool OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            GridRowElement element = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridRowElement;
            if (element != null)
            {
                this.GridControl.Columns[0].IsCurrent = true;
                this.GridControl.MasterView.TableAddNewRow.IsCurrent = true;
                this.GridControl.BeginEdit();
                return true;
            }
     
            return base.OnMouseDown(e);
        }
    }

    Please also check the attached short video showing the result on my end. With the provided solution the grid begins edit in the first column no matter if another is being edited at the moment or not.

    In case your scenario is different or the solution is not working as expected on your end, please provide me with more details about your scenario so that I can investigate it. A sample code snippet could also help. In case you want to send us your project you can do it using the ticketing system.

    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.
  9. Andy F.
    Andy F. avatar
    46 posts
    Member since:
    May 2006

    Posted 27 Dec 2017 in reply to Hristo Link to this post

    Found it.  Has to do with the first column in my grid not being visible (because it was the "Id" column), so that when the GridBehavior attempts to make it current, the grid ignores the IsCurrent setting entirely, so that the current column isn't changed.

    Better code for MyGridBehavior:

    public class MyGridBehavior : BaseGridBehavior
        {
            public override bool OnMouseDown(System.Windows.Forms.MouseEventArgs e)
            {
                GridRowElement element = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridRowElement;
                if (element != null)
                {
                    for (int i = 0; i < this.GridControl.Columns.Count; i++)
                    {
                        if (this.GridControl.Columns[i].IsVisible)
                        {
                            this.GridControl.Columns[i].IsCurrent = true;
                            break;
                        }
                    }
     
                    this.GridControl.MasterView.TableAddNewRow.IsCurrent = true;
                    this.GridControl.BeginEdit();
                    return true;
                }
     
                return base.OnMouseDown(e);
            }
        }

     

  10. Hristo
    Admin
    Hristo avatar
    1126 posts

    Posted 28 Dec 2017 Link to this post

    Hello Andy,

    Thank you for the update.

    Indeed the code snippet you shared seems valid and as long as it handles your actual scenario you can go ahead with it.

    Let me know if you have other questions.

    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.
Back to Top