Selectionchanged event firing

8 posts, 1 answers
  1. Chang
    Chang avatar
    7 posts
    Member since:
    Sep 2015

    Posted 13 Oct 2015 Link to this post

    I am new to Telerik and is trying to run the selection changed event for my gridview when select different rows. I have the code for the function, but I do not have the code to call this event. I know the event code for the button is generated in the designer.cs when I double click on the button in the form, but how do I generate an event for selection changed and other events which I cannot see? Do I have to add it in to the Designer manually?

     

    private void gvUser_SelectionChanged(object sender, Telerik.WinControls.UI.SelectionChangedEventArgs e)
    {
    }​

  2. Chang
    Chang avatar
    7 posts
    Member since:
    Sep 2015

    Posted 13 Oct 2015 Link to this post

    I tried the following code.

    gvUser.SelectionChanged += new Telerik.WinControls.UI.SelectionChangedEventHandler(gvUser_SelectionChanged);

     

    it doesn't work

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 14 Oct 2015 Link to this post

    Hello Chang,

    Thank you for writing.

    The SelectinChanged event should be visible and available in the designer. For reference, I am sending you a screenshot of the setup on my end.

    In case you are trying to subscribe to the event programmatically, you would need to change the type of the delegate from SelectionChangedEventArgs to EventArgs. Please find below my code snippet for subscribing to the event at run time: 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
            this.radGridView1.SelectionChanged += radGridView1_SelectionChanged;
        }
     
        private void radGridView1_SelectionChanged(object sender, EventArgs e)
        {
            RadMessageBox.Show("Selection Changed");
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add("Name " + i, i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
            }
     
            return dt;
        }
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    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
  5. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 22 Mar in reply to Hristo Merdjanov Link to this post

    Hello,

      I have subscribed to the radGridView1_SelectionChanged event and it works as expected with the exception of when a filter is applied.  I would expect that if the selected row is "filtered out" this event would fire.

    The issue I am having is that when the selection changes, I pull other records that are associated with the bound datasource for the selected item.  When filtering, I expect the selection to be none and be able to clear the previously pulled records.

    I am using Telerik Winforms version 2015.2.728.40

    Thanks,

    Francisco

  6. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 23 Mar Link to this post

    Hello Francisco,

    Thank you for writing.

    The selection in RadGridView is removed at the moment you start editing the filter cell. This is the behavior by design. The SelectionChanged event does not fire when a row has been filtered out simply because by the time the FilterChanged event has fired the SelectedRows collection had been emptied.

    In order to achieve your task, you would need to cache the selected rows every time when a filter cell is about to be edited. Then, if any of the cached rows is filtered out you can raise a custom event, for example, and be notified. I am sending you attached a project demonstrating a sample approach.

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  7. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 23 Mar in reply to Hristo Merdjanov Link to this post

    Hello Hristo,

      Thank you very much for the fast response and your explanation.  Looks like your sample workaround should be good but it is still a workaround.  I will likely implement the suggestion but wanted to note the following which doesn't make sense based on your explanation.

    void grid_FilterChanged(object sender, GridViewCollectionChangedEventArgs e)
           {
     
               bool rowisselected = (radGridView1.SelectedRows != null && radGridView1.SelectedRows.Count == 1);
               bool rowselectedisvisible = radGridView1.SelectedRows[0].IsVisible;
     
               System.Diagnostics.Debug.Write("\n grid_FilterChanged() one row selected?:" + rowisselected.ToString() + "  isvisible?" + rowselectedisvisible.ToString());
     
                
           }

    The above code should come back as false when a selected row is filtered out, but it actually shows as true even when the display is empty. 

    Thanks

    - Francisco

  8. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 23 Mar in reply to Hristo Merdjanov Link to this post

    Hello Hristo,

      I took at look at your sample approach to resolve the selection change issue.  Since I will be doing a combination of filtering by using the icon and text box on the column header as well as in code by using CompositeFilterDescriptor, I need to implement a slightly different workaround.

    private GridViewRowInfo[] cache;
     
            void grid_FilterChanged(object sender, GridViewCollectionChangedEventArgs e)
            {
                if (cache != null)
                {
                    RadGridView grid = (RadGridView)sender;
                    GridViewRowInfo[] local = new GridViewRowInfo[cache.Length];
                    cache.CopyTo(local, 0);
                    foreach (GridViewRowInfo row in local)
                    {
                        if (!grid.MasterView.ChildRows.Contains(row))
                        {
                            //For single select, just need to know that a previous row
                           //is no longer displayed.
                            //For multi-select, need to expand the logic.
                            cache = null;
     
                            //Clear the selection, this will raise the SelectionChanged()
                            grid.ClearSelection();
                            break;
                        }
                    }
                }
            }
     
     
    private void radGridView1_SelectionChanged(object sender, EventArgs e)
            {
     
                //Clear selection workaround.
                RadGridView grid = (RadGridView)sender;
     
                if (grid.SelectedRows.Count == 1)
                {
                    cache = new GridViewRowInfo[grid.SelectedRows.Count];
                    grid.SelectedRows.CopyTo(cache, 0);
                    
                    //Load the second grid with data based on first grid's selection.
                }
                else
                {
                    cache = null;
                    //Clear the second grid.   
                }
            }

    Thanks for your help,

    - Francisco

  9. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 24 Mar Link to this post

    Hello Francisco,

    Thank you for the update.

    If I understand correctly you have found a solution fitting your scenario.

    Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready