Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How To Prevent Datagrid Row Deletion

How To Prevent Datagrid Row Deletion

Scheduled Pinned Locked Moved C#
helptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anupbala
    wrote on last edited by
    #1

    I am using a windows form datagrid and a datatable is used as the datasource.I want to prevent deletion of the first row.I tried with datatable's rowdeleting event.But i was not able to reject the changes.Can any one help.I'm pasting the code that i tried Private Sub DataTable_RowDeleting(ByVal sender As System.Object, ByVal e As System.Data.DataRowChangeEventArgs) If e.Action = DataRowAction.Delete Then If dgGrid.CurrentCell.RowNumber = 0 Then e.Row.RejectChanges() CType(sender, DataTable).AcceptChanges() End If Else e.Row.AcceptChanges() End If End Sub Anup Balakrishnan

    G 1 Reply Last reply
    0
    • A Anupbala

      I am using a windows form datagrid and a datatable is used as the datasource.I want to prevent deletion of the first row.I tried with datatable's rowdeleting event.But i was not able to reject the changes.Can any one help.I'm pasting the code that i tried Private Sub DataTable_RowDeleting(ByVal sender As System.Object, ByVal e As System.Data.DataRowChangeEventArgs) If e.Action = DataRowAction.Delete Then If dgGrid.CurrentCell.RowNumber = 0 Then e.Row.RejectChanges() CType(sender, DataTable).AcceptChanges() End If Else e.Row.AcceptChanges() End If End Sub Anup Balakrishnan

      G Offline
      G Offline
      Glaxalg
      wrote on last edited by
      #2

      In order to solve the problem I had to ctreate my own DataGrid:

      public class CustomDataGrid : System.Windows.Forms.DataGrid {
      
          public CustomDataGrid() {}
      
          public bool bAllowDeleteRows {
              get { return m\_bAllowDeleteRows; }
              set { m\_bAllowDeleteRows = value; }
          }
      
          public override bool PreProcessMessage(ref System.Windows.Forms.Message msg) {
              
              Keys oKeyCode = (Keys)(msg.WParam.ToInt32() & (int)Keys.KeyCode);
              if ((oKeyCode == Keys.Delete) & (msg.Msg == 0x100) & !m\_bAllowDeleteRows){
                  MessageBox.Show ( "Rows deletion is not allowed" );
                  return true;
              }
              return base.PreProcessMessage (ref msg);
          }
      
          protected override bool ProcessDialogKey(Keys keyData) {
              if (keyData == Keys.Delete && this.IsSelected ( this.CurrentRowIndex ) && !m\_bAllowDeleteRows ){ 
                  MessageBox.Show ( "Rows deletion is not allowed" );
                  return true;
              }            
              return base.ProcessDialogKey (keyData);
          }
      
          private bool m\_bAllowDeleteRows = true;        
      

      Overitten functions prevent deleting row when a user click on delete.

      A 1 Reply Last reply
      0
      • G Glaxalg

        In order to solve the problem I had to ctreate my own DataGrid:

        public class CustomDataGrid : System.Windows.Forms.DataGrid {
        
            public CustomDataGrid() {}
        
            public bool bAllowDeleteRows {
                get { return m\_bAllowDeleteRows; }
                set { m\_bAllowDeleteRows = value; }
            }
        
            public override bool PreProcessMessage(ref System.Windows.Forms.Message msg) {
                
                Keys oKeyCode = (Keys)(msg.WParam.ToInt32() & (int)Keys.KeyCode);
                if ((oKeyCode == Keys.Delete) & (msg.Msg == 0x100) & !m\_bAllowDeleteRows){
                    MessageBox.Show ( "Rows deletion is not allowed" );
                    return true;
                }
                return base.PreProcessMessage (ref msg);
            }
        
            protected override bool ProcessDialogKey(Keys keyData) {
                if (keyData == Keys.Delete && this.IsSelected ( this.CurrentRowIndex ) && !m\_bAllowDeleteRows ){ 
                    MessageBox.Show ( "Rows deletion is not allowed" );
                    return true;
                }            
                return base.ProcessDialogKey (keyData);
            }
        
            private bool m\_bAllowDeleteRows = true;        
        

        Overitten functions prevent deleting row when a user click on delete.

        A Offline
        A Offline
        Anupbala
        wrote on last edited by
        #3

        Can you help me how this can be impleted in my project.This is very urgent for me.Waiting for response.

        G 1 Reply Last reply
        0
        • A Anupbala

          Can you help me how this can be impleted in my project.This is very urgent for me.Waiting for response.

          G Offline
          G Offline
          Glaxalg
          wrote on last edited by
          #4

          I didn't see your code but you need to do the following: 1) Create a new custom DataGrid using the code from the previous message 2) Change all instances of DataGrid in your code to a new CustomDataGrid 3) Set bAllowDeleteRows property of a CustomDataGrid to true That's all you need to do...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups