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

Selection after deleting from binding list

2 Answers 186 Views
GridView
This is a migrated thread and some comments may be shown as answers.
konrad
Top achievements
Rank 1
konrad asked on 21 Mar 2018, 01:09 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 21 Mar 2018, 01:50 PM
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.
0
konrad
Top achievements
Rank 1
answered on 21 Mar 2018, 04:33 PM
Thanks! It is working ;)
Tags
GridView
Asked by
konrad
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
konrad
Top achievements
Rank 1
Share this question
or