This is a migrated thread and some comments may be shown as answers.
Set current cell after ResetBindings
1 Answer 5 Views
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 13 Jan 2016, 10:01 PM

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;

 

    }

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 18 Jan 2016, 01:09 PM
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
Asked by
Karl B
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or