Set current cell after ResetBindings

2 posts, 0 answers
  1. Karl
    Karl avatar
    35 posts
    Member since:
    Dec 2013

    Posted 13 Jan Link to this post

    I want to set the current cell after performing a ResetBindings to allow seemless keyboard entry.  In my example code below I would like to be able to edit the Name column after tabbing out of the Age column.  The row appears highlighted but I must navigate to a different row before I can continue editing

     

    public class MyData : IComparable
           {
           public int Age { get; set; }
           public string Name { get; set; }
     
           public MyData ()
               {
               }
     
           public MyData (string name, int age)
               {
               Name = name;
               Age = age;
               }
     
           public int CompareTo (object obj)
               {
               var other = obj as MyData;
     
               if (other == null)
                   return 0;
     
               if (this.Age > other.Age)
                   return 1;
     
     
               if (this.Age < other.Age)
                   return -1;
     
               return 0;
               }
           }

    private List<MyData> m_dataSource;
    private BindingSource myDataBindingSource = new BindingSource();
     
    public Form1()
        {
        InitializeComponent();
        this.radGridView1.MasterTemplate.DataSource = myDataBindingSource;
        m_dataSource = PopulateData ();
        this.myDataBindingSource.DataSource = m_dataSource;
        radGridView1.CellEndEdit += radGridView1_CellEndEdit;
        }
     
    void radGridView1_CellEndEdit(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
        var data = this.radGridView1.CurrentRow.DataBoundItem as MyData;
        m_dataSource.Sort();
        myDataBindingSource.ResetBindings(false);
     
        if (data != null)
            {
            foreach (var row in this.radGridView1.Rows)
                {
                if (row.DataBoundItem == data)
                    {
                    row.IsCurrent = true;
                    }
                else
                    {
                    row.IsCurrent = false;
                    }
                }
            }
        }
     
    private List<MyData> PopulateData ()
        {
        List<MyData> dataList = new List<MyData> ();
     
        dataList.Add (new MyData ("A", 1));
        dataList.Add (new MyData ("B", 7));
        dataList.Add (new MyData ("C", 3));
        dataList.Add (new MyData ("D", 5));
        dataList.Add (new MyData ("E", 15));
     
        return dataList;

     

        }

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 18 Jan Link to this post

    Hi Karl,

    Thank you for writing back.

    First I want to mention that you can use the grid sorting and this way you would not need to reset the bindings:
    radGridView1.SortDescriptors.Add("Age", ListSortDirection.Ascending);

    If you want to use the BindingSource sorting you should create a custom row behavior and change the current row there:   
    protected override bool ProcessTabKey(System.Windows.Forms.KeyEventArgs keys)
    {
        var data = this.GridControl.CurrentRow.DataBoundItem as MyData;
        var res = base.ProcessTabKey(keys);
        if (data != null)
        {
            foreach (var row in this.GridControl.Rows)
            {
                if ((MyData)row.DataBoundItem == data)
                {
                    this.GridControl.CurrentRow = row;
                }
            }
        }
        this.GridControl.BeginEdit();
        return res;
    }

    You can change the default behavior like this:
    BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
    gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
    gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomRowBehavior());

    I hope this helps.

    Regards,
    Dimitar
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top