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

Selectionchanged event firing

7 Answers 1635 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chang
Top achievements
Rank 1
Chang asked on 13 Oct 2015, 09:30 AM

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)
{
}​

7 Answers, 1 is accepted

Sort by
0
Chang
Top achievements
Rank 1
answered on 13 Oct 2015, 11:42 AM

I tried the following code.

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

 

it doesn't work

0
Accepted
Hristo
Telerik team
answered on 14 Oct 2015, 03:03 PM
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
0
Francisco
Top achievements
Rank 1
answered on 22 Mar 2016, 05:35 PM

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

0
Hristo
Telerik team
answered on 23 Mar 2016, 12:11 PM
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.
0
Francisco
Top achievements
Rank 1
answered on 23 Mar 2016, 04:38 PM

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

0
Francisco
Top achievements
Rank 1
answered on 23 Mar 2016, 06:11 PM

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

0
Hristo
Telerik team
answered on 24 Mar 2016, 04:12 PM
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.
Tags
GridView
Asked by
Chang
Top achievements
Rank 1
Answers by
Chang
Top achievements
Rank 1
Hristo
Telerik team
Francisco
Top achievements
Rank 1
Share this question
or