77using System . Runtime ;
88using System . Windows . Forms ;
99using System . Xml . Linq ;
10+ using static System . Windows . Forms . AxHost ;
1011
1112
1213namespace PackedTables . Viewer {
@@ -124,6 +125,8 @@ private void DoCloseFileTable() {
124125 _workingPack = null ;
125126 toolStripButton1 . Enabled = false ;
126127 toolStripButton2 . Enabled = false ;
128+ toolStripButton3 . Enabled = false ;
129+ toolStripButton4 . Enabled = false ;
127130 splitContainer3_Panel1_Resize ( null , null ) ;
128131 }
129132
@@ -299,7 +302,7 @@ private void cbType_SelectedIndexChanged(object sender, EventArgs e) {
299302 }
300303 }
301304
302- private void ClearVrMain ( ) {
305+ private void ClearVrMain ( ) {
303306 if ( vrMain . Rows . Count > 0 ) { vrMain . Rows . Clear ( ) ; }
304307 if ( vrMain . Columns . Count > 0 ) { vrMain . Columns . Clear ( ) ; }
305308 }
@@ -348,11 +351,31 @@ private void LoadDataGridViewFromTable(TableModel table) {
348351 }
349352 vrMain . RowCount = _table . Rows . Count ;
350353 _table . OrderByColumnName = OrderByColumnName ;
354+ _table . StateChanged -= CurrentTableStateChanged ;
355+ _table . StateChanged += CurrentTableStateChanged ;
351356 vrMain . ResumeLayout ( false ) ;
352357 vrMain . Enabled = true ;
353358 if ( ! vrMain . Visible ) { vrMain . Visible = true ; }
354359 }
355360
361+ private void CurrentTableStateChanged ( object ? sender , StateChangedEventArgs e ) {
362+ SetToolstripButtonsByState ( e . NewState ) ;
363+ }
364+
365+ private void SetToolstripButtonsByState ( TableState state ) {
366+ if ( state == TableState . Browse ) {
367+ toolStripButton1 . Enabled = true ; // Add
368+ toolStripButton2 . Enabled = true ; // Delete
369+ toolStripButton3 . Enabled = false ; // Post/Edit
370+ toolStripButton4 . Enabled = false ; // Cancel
371+ } else {
372+ toolStripButton1 . Enabled = false ; // Add
373+ toolStripButton2 . Enabled = false ; // Delete
374+ toolStripButton3 . Enabled = true ; // Post
375+ toolStripButton4 . Enabled = true ; // Cancel
376+ }
377+ }
378+
356379 private void vrMain_CellValueNeeded ( object sender , DataGridViewCellValueEventArgs e ) {
357380 try {
358381 var columnName = vrMain . Columns [ e . ColumnIndex ] . Name ;
@@ -377,14 +400,30 @@ private void vrMain_CellValueNeeded(object sender, DataGridViewCellValueEventArg
377400
378401 private void vrMain_CellValuePushed ( object sender , DataGridViewCellValueEventArgs e ) {
379402 try {
403+ if ( _table == null ) return ;
404+ if ( e . RowIndex < 0 || e . ColumnIndex < 0 || e . RowIndex > vrMain . RowCount || e . ColumnIndex >= vrMain . Columns . Count ) {
405+ LogMsg ( "Invalid cell index in CellValuePushed." ) ;
406+ return ;
407+ }
380408 var columnName = vrMain . Columns [ e . ColumnIndex ] . Name ;
381409 var rowNumber = e . RowIndex + 1 ; // tables are 1-based in PackedTables
382410 if ( _table != null && _table . RowIndex . TryGetValue ( rowNumber , out var tblIndex ) && tblIndex != 0 ) {
383- if ( _table . Rows . TryGetValue ( tblIndex , out var row ) ) {
384- if ( row != null ) {
385- row [ columnName ] . Value = e . Value ?? "" ;
411+ if ( _table . Current == null || _table . Current . Id != tblIndex ) {
412+ LogMsg ( "error: On cell push not in edit mode." ) ;
413+ if ( _table . Rows . TryGetValue ( tblIndex , out var row ) ) {
414+ if ( row != null ) {
415+ row [ columnName ] . Value = e . Value ?? "" ;
416+ }
417+ }
418+ } else {
419+ // We are in edit mode, so we can safely update the current row so maybe we can cancel or post it later.
420+ if ( _table . Current != null ) {
421+ _table . Current [ columnName ] . Value = e . Value ?? "" ;
422+ } else {
423+ LogMsg ( $ "Error: Current row or field '{ columnName } ' not found.") ;
386424 }
387425 }
426+
388427 }
389428 } catch ( Exception ex ) {
390429 LogMsg ( ex . Message ) ;
@@ -444,7 +483,7 @@ private void tvMenu_Opening(object sender, System.ComponentModel.CancelEventArgs
444483 private void addTableToolStripMenuItem_Click ( object sender , EventArgs e ) {
445484 if ( _workingPack == null ) return ;
446485 string newTableName = $ "Table{ _workingPack . TableCount + 1 } ";
447- var newTable = _workingPack . AddTable ( newTableName ) ;
486+ var newTable = _workingPack . AddTable ( newTableName ) ;
448487 LoadTreeViewFromWorkingPack ( ) ;
449488 }
450489 private void removeTableToolStripMenuItem_Click ( object sender , EventArgs e ) {
@@ -528,41 +567,105 @@ private void splitContainer3_Panel2_Resize(object sender, EventArgs e) {
528567 }
529568
530569 private void vrMain_SelectionChanged ( object sender , EventArgs e ) {
570+ if ( _workingPack == null || _table == null ) return ;
531571 var selectedRows = vrMain . SelectedRows ;
532- if ( selectedRows . Count > 0 ) {
533- toolStripButton2 . Enabled = true ;
572+ if ( selectedRows . Count == 0 ) {
573+ SetToolstripButtonsByState ( _table . State ) ;
534574 } else {
535- toolStripButton2 . Enabled = false ;
575+ if ( selectedRows . Count == 1 ) {
576+ // Single row selected, enable edit/post
577+ SetToolstripButtonsByState ( _table . State ) ;
578+ } else {
579+ // Multiple rows selected, disable edit/post
580+ toolStripButton2 . Enabled = true ;
581+ toolStripButton3 . Text = "Delete" ;
582+ }
536583 }
537584 }
538585
539586 private void toolStripButton1_Click ( object sender , EventArgs e ) {
587+ // AddRow and make it Current row in tabel update the grid focus.
540588 if ( _workingPack == null || _table == null ) return ;
541589 _table . AddRow ( ) ;
542590 LoadDataGridViewFromTable ( _table ) ;
591+ SetToolstripButtonsByState ( _table . State ) ;
592+ if ( vrMain . Rows . Count > 0 ) {
593+ vrMain . CurrentCell = vrMain . Rows [ vrMain . RowCount - 1 ] . Cells [ 0 ] ; // Set focus to the last row
594+ vrMain . BeginEdit ( true ) ; // Start editing the first cell of the new row
595+ }
543596 }
544597
545598 private void toolStripButton2_Click ( object sender , EventArgs e ) {
546- if ( _workingPack == null || _table == null || vrMain . SelectedRows . Count == 0 ) return ;
547- var selectedRows = vrMain . SelectedRows ;
548- foreach ( DataGridViewRow row in selectedRows ) {
549- if ( row . Index < 0 || row . Index >= vrMain . RowCount ) continue ; // skip invalid rows
550- var rowNumber = row . Index + 1 ; // PackedTables are 1-based
551- if ( _table . RowIndex . TryGetValue ( rowNumber , out int rowId ) ) {
552- if ( _table . Rows . TryGetValue ( rowId , out var tableRow ) ) {
553- _table . RemoveRow ( tableRow ) ;
554- vrMain . Rows . RemoveAt ( row . Index ) ;
599+ // Delete for the current row in tabel.
600+ if ( _workingPack == null || _table == null ) return ;
601+ if ( _table . State == TableState . Browse ) {
602+ var selectedRows = vrMain . SelectedRows ;
603+ foreach ( DataGridViewRow row in selectedRows ) {
604+ if ( row . Index < 0 || row . Index >= vrMain . RowCount ) continue ; // skip invalid rows
605+ var rowNumber = row . Index + 1 ; // PackedTables are 1-based
606+ if ( _table . RowIndex . TryGetValue ( rowNumber , out int rowId ) ) {
607+ if ( _table . Rows . TryGetValue ( rowId , out var tableRow ) ) {
608+ _table . RemoveRow ( tableRow ) ;
609+ vrMain . Rows . RemoveAt ( row . Index ) ;
610+ }
555611 }
556612 }
557613 }
558614 LoadDataGridViewFromTable ( _table ) ;
615+ SetToolstripButtonsByState ( _table . State ) ;
616+ }
617+ private void toolStripButton3_Click ( object sender , EventArgs e ) {
618+ // POST processing for the current row in tabel.
619+ if ( _workingPack == null || _table == null ) return ;
620+ if ( _table . State != TableState . Browse ) {
621+ vrMain . EndEdit ( ) ; // Ensure any edits are saved
622+ if ( _table . State != TableState . Browse ) {
623+ _table . Post ( ) ; // Post changes to the table
624+ }
625+ }
626+ SetToolstripButtonsByState ( _table . State ) ;
627+ }
628+
629+ private void toolStripButton4_Click ( object sender , EventArgs e ) {
630+ // Cancel processing for the current row in tabel.
631+ if ( _workingPack == null || _table == null ) return ;
632+ if ( _table . State != TableState . Browse ) {
633+ _table . Cancel ( ) ;
634+ vrMain . CancelEdit ( ) ;
635+ }
636+ SetToolstripButtonsByState ( _table . State ) ;
559637 }
560638
561- private void vrMain_CellEnter ( object sender , DataGridViewCellEventArgs e ) {
562- if ( e . RowIndex < 0 || e . RowIndex >= vrMain . RowCount ) return ; // prevent out of range
563- if ( _table == null ) return ;
564- toolStripButton1 . Enabled = true ;
565- toolStripButton2 . Enabled = true ;
639+ private void vrMain_CellBeginEdit ( object sender , DataGridViewCellCancelEventArgs e ) {
640+ var rowid = e . RowIndex + 1 ; // PackedTables are 1-based, so adjust the index
641+ if ( _table == null || ! _table . RowIndex . TryGetValue ( rowid , out int rowId ) ) {
642+ e . Cancel = true ; // No valid row to edit
643+ return ;
644+ } else {
645+ if ( _table . Current == null || _table . Current [ "Id" ] . Value . AsInt32 ( ) != rowId ) {
646+ _table ! . FindFirst ( "Id" , rowId ) ; // Ensure the row is loaded for editing
647+ }
648+ if ( _table . State == TableState . Browse ) {
649+ _table . Edit ( ) ; // Switch to edit mode if not already
650+ }
651+ SetToolstripButtonsByState ( _table . State ) ;
652+ e . Cancel = false ; // Allow editing
653+ }
654+ }
655+
656+ private void vrMain_RowLeave ( object sender , DataGridViewCellEventArgs e ) {
657+ if ( _workingPack == null || _table == null ) return ;
658+ if ( e . RowIndex < 0 || e . RowIndex > vrMain . RowCount ) return ; // Invalid row index
659+ if ( _table . State == TableState . Browse ) return ; // No need to cancel if in browse state
660+
661+ if ( ! vrMain . IsCurrentRowDirty && ! vrMain . IsCurrentCellInEditMode ) {
662+ toolStripButton4_Click ( sender , e ) ; // Cancel the current edit
663+ return ;
664+ }
665+
666+ if ( vrMain . IsCurrentRowDirty && vrMain . IsCurrentCellInEditMode ) {
667+ toolStripButton3_Click ( sender , e ) ;
668+ }
566669 }
567670 }
568671}
0 commit comments