'Prevent raising SelectionChanged event on a DataRow's Button click within a C# DataGridView

I have a DataGridView which has a Button column.

Each record in the DataGridView will have a separate Button. A QueueMusic object containing Row-specific data should be queued upon clicking a Row's Button.

I currently have this working by placing a custom class (QueueMusic) onto a Queue collection using that collection's .Enqueue() method.

I have two event handler methods.

  1. DataGridViewAllMusic_SelectionChanged method, which begins playing music associated the current Button's Row.

  2. DataGridViewAllMusic_CellClick method, which handles queueing the playlist (defined within the QueueMusic class) that is associated with the current Button's Row.

The Problem

  • Once music is playing, each subsequent click of a Row's Button interrupts the currently playing music with music defined by the newest Row's QueueMusic object.

I have a class QueueMusic.

    internal class QueueMusic
    {
        public string Url { get; set; }
        public int RowIndex { get; set; }

        public static Queue<QueueMusic> queulist = new Queue<QueueMusic>();
    }

and a DataGridView CellClickEvent

    private void DataGridViewAllMusic_CellClick(object sender, DataGridViewCellEventArgs 
     e)
    {

      var senderGrid = (DataGridView)sender;

      if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 
       0)
       {
            QueueMusic qm = new QueueMusic();
            qm.RowIndex = e.RowIndex;
            qm.Url = DataGridViewAllMusic.Rows[e.RowIndex].Cells[2].Value.ToString();
                    
            QueueMusic.queulist.Enqueue(qm);
       }

     }


    private void DataGridViewAllMusic_SelectionChanged(object sender, EventArgs e)
    {
      play();
    }

Desired Behavior

How can I prevent a Row's Button click event from being handled by DataGridViewAllMusic_SelectionChanged when music is already playing?

The desired behavior is to queue the next playlist in the background without inturrupting the currently playing playlist.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source