How To Prevent Datagrid Row Deletion
-
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
-
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
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.
-
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.
-
Can you help me how this can be impleted in my project.This is very urgent for me.Waiting for response.
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) SetbAllowDeleteRows
property of aCustomDataGrid
to true That's all you need to do...