Selection after deleting from binding list

3 posts, 1 answers
  1. Dominik
    Dominik avatar
    82 posts
    Member since:
    Dec 2012

    Posted 21 Mar 2018 Link to this post

    Hi, 

    Version: 2017.3.1017.20

    Code to reproduce problem below. Just create new form and copy-paste code below.

    public partial class Form : System.Windows.Forms.Form
      {
          public Form()
          {
              InitializeComponent();
          }
           
          private void Form_Load(object sender, EventArgs e)
          {
              RadGridView grid = new RadGridView();
              grid.Dock = DockStyle.Left;
              grid.SortDescriptors.Add("Id", ListSortDirection.Ascending);
              grid.SelectionMode = GridViewSelectionMode.FullRowSelect;
     
              BindingList <X> bindingList = new BindingList<X>();
              bindingList.Add(new X() { Id = 1, Name = "Row 1" });
              bindingList.Add(new X() { Id = 5, Name = "Row 5" });
              bindingList.Add(new X() { Id = 3, Name = "Row 3" });
              bindingList.Add(new X() { Id = 4, Name = "Row 4" });
              bindingList.Add(new X() { Id = 2, Name = "Row 2" });
              bindingList.Add(new X() { Id = 6, Name = "Row 6" });
              bindingList.Add(new X() { Id = 7, Name = "Row 7" });
              bindingList.Add(new X() { Id = 8, Name = "Row 8" });
              bindingList.Add(new X() { Id = 9, Name = "Row 9" });
              bindingList.Add(new X() { Id = 10, Name = "Row 10" });
     
              Controls.Add(grid);
     
              grid.DataSource = bindingList;
     
              RadButton radButton = new RadButton();
              radButton.Text = "Delete row";
              radButton.Dock = DockStyle.Right;
              radButton.Click += delegate
              {
                  var selected = (X)grid.CurrentRow.DataBoundItem;
                  bindingList.Remove(selected);
              };
     
              Controls.Add(radButton);
          }
      }
     
      public class X
      {
          public Int32 Id { get; set; }
          public String Name { get; set; }
      }

     

    My grid is connected to binding list. Clicking on the button remove currently selected row from binding list. 

    The problem is: Sometimes after deleting, nearest row is selected. Sometimes grid is clearing selection.

    Please see attached gif.

    I would like the next record to be always selected after deleting current one.

    If deleted row was last row, it should select previous one.

    I was trying to use GridNavigator.SelectPreviousRow(1) and GridNavigator.SelectNextRow(1) but it do not work, becouse sometimes after deleting row grid is clearing selection.

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2949 posts

    Posted 21 Mar 2018 Link to this post

    Hi Dominik,

    You can use the RadGridView API to remove the row and you will get the expected behavior:
    var selected = radGridView1.CurrentRow;
    radGridView1.Rows.Remove(selected);

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    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.
  3. Dominik
    Dominik avatar
    82 posts
    Member since:
    Dec 2012

    Posted 21 Mar 2018 in reply to Dimitar Link to this post

    Thanks! It is working ;)
Back to Top