Click here to Skip to main content
15,891,033 members
Articles / Operating Systems / Windows
Tip/Trick

DataGridView Row Delete in CellSelect Mode

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Apr 2013CPOL 8.7K   2  
Event to delete rows by selected cells.

I was recently helping a coworker on a gridview context menu delete function. His was in CellSelect mode and he didn’t want to change it. We searched for and found that people were insisting on FullRowSelect. I played around and got this to work. It allows row(s) to be deleted in either modes. I had an attached database function which I removed and left a comment. The trick is to get the row from the set of selected cells and keep the last row deleted to avoid duplicate deletes.

VB
BegSr DeleteMenu1_Click Access(*Private)  Event(*this.Deletemenu1.Click)
    DclSrParm sender *object
    DclSrParm eventArgs System.EventArgs
    DclFld Rowy *Integer4 
    DclFld Cellx *Integer4 
    DclFld Last_Rowy *Integer4 
    *this.Cursor = System.Windows.Forms.Cursors.WaitCursor
    if DataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect | 
      System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect 
        Last_Rowy = -1
        Do FromVal(0) ToVal(DataGridView.SelectedCells.Count - 1) Index(Cellx) // Cell Index
            Rowy = DataGridView.SelectedCells(Cellx).Rowindex  // get row index
            if Last_Rowy <> Rowy
                Last_Rowy = Rowy
                // do any physical database deletes if required here
                DataGridView.Rows.RemoveAt(Rowy)
            endif
        enddo
    endif
    *this.Cursor = System.Windows.Forms.Cursors.Arrow
EndSr

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Writing code for 00100010 years.

Comments and Discussions

 
-- There are no messages in this forum --