Disabling gridview column and collapsing button in CommandColumn

1 Answer 43 Views
GridView
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Patgat asked on 28 Sep 2023, 02:31 PM | edited on 28 Sep 2023, 02:38 PM

Hi,

I am having two issues with a RadGridView and need some help.

In the attached movie, there is a gridView with two "optional" columns : one with check boxes and one with buttons.

This is to allow a user to modify values in the associated DB Table either individually with the buttons or as a group via the check boxes.

However, I want only one column active at any time. The selection being done via the upper left label.

Note that  I do not want the CommandColumn to be hiden, as the buttons labels also convey status information.

The problems are as follow :

1- when the form is loaded, I only want to show the lines where buttons in the "ready for update" state (written "Mettre à jour"). So I hide the other rows labelled "Not Ready". Doing this in the CellFormatting event works, but due to the virtualisation, as you can see in the attached movie, if the user move the colums too fast, the Not ready cells are temporarily displayed ( I tried a MasterTemplate.Refresh but this crash the form load with an index invalid exception in Telerik code).

How can I solved this ?

2 - when I click on the label (used as a button) to show the Check box column, it should change color in grey. As you can see, it does it (on the whole column as expected) but only if I click on any of the column button.

Here is the CellFormatting code :

  private void gvMajBoutique_CellFormatting(object sender, CellFormattingEventArgs e)
  {
      if (e.CellElement is not GridCommandCellElement commandCell) return;
      var row = commandCell.RowInfo;
      if (row == null) return;

      string ugs = row.Cells["Sku"].Value.ToString();
      if (ugs != null && SuiviDesModifications.ContainsKey(ugs))
      {
          StatusModifsPrixBoutique state = SuiviDesModifications[ugs];

          if (state == null) return;

          if (state.CurrentState == ChangeButtonState.UpdateReady)
          {
              commandCell.CommandButton.Text = state.CurrentButtonName;
              row.IsVisible = true;
          }

          else if (state.CurrentState == ChangeButtonState.UpdateForbidden)
          {
              row.IsVisible = false;
          }

          if (StatusColumnEnabled)
          {
              commandCell.CommandButton.ButtonFillElement.BackColor = Color.PapayaWhip;
              commandCell.CommandButton.ButtonFillElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
              commandCell.CommandButton.Enabled = true;
          }
          else
          {
              commandCell.CommandButton.ButtonFillElement.BackColor = Color.LightGray;
              commandCell.CommandButton.ButtonFillElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
              commandCell.CommandButton.Enabled = false;
          }
          // This crash the form //gvMajBoutique.MasterTemplate.Refresh();
      }
  }

Thanks for your answers

Patrick

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 29 Sep 2023, 08:01 AM

Hello, Patrick,

Straight to your questions:

1) Note, RadGridView uses virtualization for its cells and rows. This means that the visual elements are reused during scrolling, filtering and other operations with the grid. More information on this topic is available here:

UI Virtualization - WinForms GridView Control - Telerik UI for WinForms
Formatting Cells - WinForms GridView Control - Telerik UI for WinForms

Because of the virtualization it is suitable to use formatting events in order to customize the look of the grid. It is important to provide an else clause and reset all the introduced customizations in order to show the correct results when scrolling, etc. I would recommend you to carefully check the if/else clauses in the CellFormatting event. I provide the following code snippet which works as expected on my end:

 private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs 
 {
     var commandCell = e.CellElement as GridCommandCellElement;
     if (commandCell != null)
     {
         var row = commandCell.RowInfo;
         if (row.Index % 2 == 0)
         {
             e.CellElement.RowElement.RowInfo.IsVisible = false;
         }
         else
         {
             e.CellElement.RowElement.RowInfo.IsVisible = true;
         }
     }
 }

From the provided code snippet I can see that you call Refresh method in CellFormatting event which fires for every data cell in the grid. The Refresh method is used to visually refresh the control. If needed, it is suitable to call it just once since it triggers the CellFormatting event again. Hence, it is not correct to use the Refresh method in the CellFormatting event.

2) Here, in order to force the grid to update colors after clicking on the label (used as a button) it is suitable to use MasterTemplate.Refresh(). 

I hope this helps. Should you have any other questions do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Patgat
Top achievements
Rank 2
Iron
Iron
Iron
commented on 02 Oct 2023, 12:05 PM

Thanks Nadya,

MasterTemplate.Refresh() outside the CellFormatting event works fine.

Regarding the other issue I found a way to address it differently as the  CellFormatting event comes too late.

Regards

Patrick

Tags
GridView
Asked by
Patgat
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or