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

How to set focus to first column in add line

9 Answers 604 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ruth Goldberg
Top achievements
Rank 1
Ruth Goldberg asked on 01 Mar 2010, 04:04 PM
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.

9 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 02 Mar 2010, 07:35 AM
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.
0
Ruth Goldberg
Top achievements
Rank 1
answered on 04 Mar 2010, 08:54 PM
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()  
  
0
Jack
Telerik team
answered on 05 Mar 2010, 03:49 PM
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.
0
Ruth Goldberg
Top achievements
Rank 1
answered on 08 Mar 2010, 07:11 PM
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#.
0
Jack
Telerik team
answered on 09 Mar 2010, 10:10 AM
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.
0
Andy F.
Top achievements
Rank 1
Iron
answered on 19 Dec 2017, 05:09 PM

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.

0
Hristo
Telerik team
answered on 20 Dec 2017, 07:52 AM
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.
0
Andy F.
Top achievements
Rank 1
Iron
answered on 27 Dec 2017, 08:50 PM

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

 

0
Hristo
Telerik team
answered on 28 Dec 2017, 08:10 AM
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.
Tags
GridView
Asked by
Ruth Goldberg
Top achievements
Rank 1
Answers by
Jack
Telerik team
Ruth Goldberg
Top achievements
Rank 1
Andy F.
Top achievements
Rank 1
Iron
Hristo
Telerik team
Share this question
or