This is a migrated thread and some comments may be shown as answers.

Remove Conditional formatting after button click

8 Answers 294 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 13 Jan 2020, 10:40 PM

Hi,

I have a grid-view with multiple grids working together, and one of the tasks at hand is if a value changes in a cell in one grid, then the main grid has to highlight any objects that shared that data that were affected by the change. The customer then needs to compile that data and at that point, the row for that object needs to have the highlight removed. I am able to get the conditional formatting for the highlight to work, but removing it from the entire row after the button click is proving to be a challenge. currently my code is set to do this using the following:

      public void GridHighLighterSetConditions()
      {
         int x = -1;
         double Index1 = - 1;
         double Index2 = - 1;
         double Index3 = - 1;
         string MIDindex = "";
         int RowIndex = 0;
         int MID = 0;


         foreach (GridViewDataRowInfo Index in MainGridView.SelectedRows)
         {

            if (MainGridView.SelectedRows.Count > 0)
            {
               for (x = 0; x < MainGridView.SelectedRows.Count; x++)
               {
                  RowIndex = MainGridView.SelectedRows[x].Index;

                  MIDindex = MainGridVIew.Rows[RowIndex].Cells[0].Value.ToString();

                  MID = int.Parse(MIDindex);
               }
               break;
            }
         }

         foreach (GridViewDataRowInfo Event in GridView2.Rows)

         {

            if (GridView2.SelectedRows.Count > 0)
            {
               for (x = 0; x < GridView2.SelectedRows.Count; x++)
               {
                  Index1 = GridView2.SelectedRows[x].Index + 1;
               }
               break;
            }
         }

         foreach (GridViewDataRowInfo Vevent in GridView3.Rows)
         {

            if (GridView3.SelectedRows.Count > 0)
            {
               for (x = 0; x < GridView3.SelectedRows.Count; x++)
               {
                  Index2 = GridView3.SelectedRows[x].Index + 1;
               }
               break;
            }
         }

         foreach (GridViewDataRowInfo REvent in GridView4.Rows)
         {

            if (GridView4.SelectedRows.Count > 0)
            {
               for (x = 0; x < GridView4.SelectedRows.Count; x++)
               {
                  Index3 = GridView4.SelectedRows[x].Index + 1;
               }
               break;
            }
         }

         ConditionalFormattingObject HL_1 = new ConditionalFormattingObject("Highlighter", ConditionTypes.Equal, Index1.ToString(), "", true);
         Font newFont = new Font("Arial", 8f, FontStyle.Bold);
         HL_1.CellFont = newFont;
         HL_1.CellForeColor = Color.DarkRed;
         HL_1.RowBackColor = Color.Gold;

         ConditionalFormattingObject HL_2= new ConditionalFormattingObject("Highlighter", ConditionTypes.Equal, Index2.ToString(), "", true);
         Font newPBVFont = new Font("Arial", 8f, FontStyle.Bold);
         HL_2.CellFont = newPBVFont;
         HL_2.CellForeColor = Color.DarkRed;
         HL_2.RowBackColor = Color.Gold;


         ConditionalFormattingObject HL_3 = new ConditionalFormattingObject("Highlighter", ConditionTypes.Equal, Index3.ToString(), "", true);
         Font newRVFont = new Font("Arial", 8f, FontStyle.Bold);
         HL_3.CellFont = newRVFont;
         HL_3.CellForeColor = Color.DarkRed;
         HL_3.RowBackColor = Color.Gold;

         ConditionalFormattingObject No_HL = new ConditionalFormattingObject("No Highlighter", ConditionTypes.Equal, MIDindex, "", true);
         Font newUMCFont = new Font("Arial", 8f, FontStyle.Regular);
         No_HL.CellFont = newUMCFont;
         No_HL.RowBackColor = Color.White;
         No_HL.CellForeColor = Color.Black;

         if (Index1 > Index2 && Index1 > Index3)
         {
            MainGridView.Columns["Column1"].ConditionalFormattingObjectList.Add(HL_1);

            GridView2.ClearSelection();
         }

         else if (Index2 > Index1 && Index2 > Index3)
         {
            MainGridView.Columns["column2"].ConditionalFormattingObjectList.Add(HL_2);

            GridView3.ClearSelection();
         }

         else if (Index3 > Index1 && Index3 > Index2)
         {
            MainGridView.Columns["Column3"].ConditionalFormattingObjectList.Add(HL_3);

            GridView4.ClearSelection();
         }
         else if (MIDindex != null && MID > Index1 && MID > Index2 && MID > Index3)
         {
            Index1 = -1;
            Index2 = -1;
            Index3 = -1;

            MainGridView.Columns["Column0"].ConditionalFormattingObjectList.Add(No_HL);
            

            MainGridView.ClearSelection();
            MainGridView.Refresh();
         }
         return;
      }

 

I Feel as though there must be a much easier way to do this.

Thanks.

8 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
answered on 13 Jan 2020, 10:47 PM

The button  click would trigger the 

 else if (MIDindex != null && MID > Index1 && MID > Index2 && MID > Index3)
         {
            Index1 = -1;
            Index2 = -1;
            Index3 = -1;

            MainGridView.Columns["Column0"].ConditionalFormattingObjectList.Add(No_HL);
            

            MainGridView.ClearSelection();
            MainGridView.Refresh();
         }

0
David
Top achievements
Rank 1
answered on 14 Jan 2020, 03:02 PM
Update* I am using MainGridView.Columns["Column"].ConditionalFormattingObjectList.Clear(); for the time being but it clears all rows and I need to clear only the currently selected row after the user clicks a button.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Jan 2020, 11:44 AM

Hello, David,    

According to the provided information, I think that using the RowFormatting or CellFormatting events would be a more appropriate solution for your case. You can use the data row's Tag property whether it should be highlighted or not and check the row's Tag value in the formatting event to apply the desired style. Once a row is updated, you can simply call its InvalidateRow method of the GridViewRowInfo and thus the respective formatting event will be forced and the appropriate style applied.

Additional information how to use the RowFormatting or CellFormatting events is available here: 
https://docs.telerik.com/devtools/winforms/controls/gridview/rows/formatting-rows
https://docs.telerik.com/devtools/winforms/controls/gridview/cells/formatting-cells

Please give this approach a try and see how it works for your case.

I hope this information helps. If you need any further assistance please don't hesitate to contact me.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
answered on 21 Jan 2020, 08:17 PM

Thanks For Responding Dess,

I attempted to use the RowFormatting and CellFormatting as you suggested, but it's not working.

Just to clarify, I have Multiple RadGridView's each tabbed separately. What I want to do is, if information is changed in any of the cells for the currently selected row in RadGridView2 I want it to highlight that data row's index to be Changed to red in the corresponding cell in RadGridView1 as well as highlight that row in RadGridView1. Which I was able to accomplish with code I provided above.

But when the user clicks a button that will compile the information in the highlighted row in RadGridView1 after they select that row, if it compiles successfully, I want to have the currently selected row in RadGridView1 go back to normal (no highlights or changed font) without any of the info in the Currently Selected row changing.

is this possible?

Thanks in advance.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 Jan 2020, 10:43 AM
Hello, David,  

I have prepared a sample code snippet demonstrating the idea of handling the CellFormatting event. The attached gif file illustrates better the obtained result. 
        public RadForm1()
        {
            InitializeComponent();

            this.radGridView1.CellFormatting += radGridView1_CellFormatting;

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("CReatedOn", typeof(DateTime));
            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add(i, "Item" + i, DateTime.Now.AddDays(i));
            }
            this.radGridView1.DataSource = dt;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView2.DataSource = dt;
            this.radGridView2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

            this.radGridView2.CellValueChanged += radGridView2_CellValueChanged;
        }

        Font f = new Font("Arial", 8f, FontStyle.Bold);

        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Row.Cells[e.ColumnIndex].Tag != null && e.Row.Cells[e.ColumnIndex].Tag == "changed")
            {
                e.CellElement.ForeColor = Color.Red;
                e.CellElement.Font = f;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.FontProperty, ValueResetFlags.Local);
            }
        }

        private void radGridView2_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            this.radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = "changed";
            this.radGridView1.Rows[e.RowIndex].InvalidateRow();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            if (this.radGridView1.CurrentRow!=null)
            {
                foreach (GridViewCellInfo cell in this.radGridView1.CurrentRow.Cells)
                {
                    cell.Tag = null;
                }
                this.radGridView1.CurrentRow.InvalidateRow();
            }
        }
You can find attached the complete project as well. Please give it a try and see whether it works for your case.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
answered on 22 Jan 2020, 06:17 PM

Thank you for your reply Dess,

That was an awesome example of how to use the tagging feature, but it's still not quite what I'm looking for. I tried tweaking the code you provided to do what I needed, but it is still not cooperating. I attached some screenshots to give you an example of exactly what I'm attempting to do.

0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Jan 2020, 08:27 AM

Hello, David,  

Thank you for providing the screenshot. According to it, you want to get the Index cell's value of the edited row and highlight the cells on the right grid that refer to the same index of the affected row. Actually, you can use a similar approach as the one illustrated in the previously prvided example. You just need to iterate all rows on the right grid and check whether the respective row's Index is equal to theaffected row's Index. Then, mark the row with the Tag property as in the project.

I have updated the sample project. The achieved result is illustrated in the attached gif file. 

Should you have further questions please let me know.

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
answered on 27 Jan 2020, 05:20 PM
Thanks so much for your help Dess that worked perfectly! I marked this as closed
Tags
GridView
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or