[RadGridView] help with unchecking one column when another is checked

1 Answer 187 Views
GridView
Memo
Top achievements
Rank 1
Iron
Veteran
Memo asked on 16 May 2022, 02:38 AM

.NET Framework 4.8

Telerik controls 2022 R1

 

Only one of two checkbox columns can be checked at any given time for any given row. 

What I'm experiencing is when one column is checked and the other is selected, both are then unchecked including the one clicked.

 


 

Sample project illustrating the issue is attached and the event handler code is below.  

 

What am I doing wrong?  Any help would be most appreciated.

 

TIA

 

Event handler code:


        private void Dgv_MenuItems_ValueChanged(object sender, EventArgs e)
        {
            if (_settingItemAvail)
            {
                return;
            }

            if (sender != null && sender is RadCheckBoxEditor checkBoxEditor)
            {
                var cell = checkBoxEditor.EditorElement.Parent as GridDataCellElement;
                var cellName = cell.ColumnInfo.Name;
                var row = cell.RowInfo;

                if (row.Index < 0)
                {
                    return;
                }

                _settingItemAvail = true;

                switch (cellName)
                {
                    case "Unavailable":
                        if (checkBoxEditor.Value.ToString() == "On")
                        {
                            if ((bool)dgv_MenuItems.Rows[row.Index].Cells["Available"].Value)
                            {
                                dgv_MenuItems.Rows[row.Index].Cells["Available"].Value = false;
                            }
                        }
                        break;

                    case "Available":
                        if (checkBoxEditor.Value.ToString() == "On")
                        {
                            if ((bool)dgv_MenuItems.Rows[row.Index].Cells["Unavailable"].Value)
                            {
                                dgv_MenuItems.Rows[row.Index].Cells["Unavailable"].Value = false;
                            }

                        }
                        break;
                }

                _settingItemAvail = false;
            }
        }

1 Answer, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 May 2022, 10:05 AM
Hello, Memo,   

The provided sample project is greatly appreciated. If I understand your requirement correctly, you need to achieve only one of the GridViewCheckBoxColumns to be toggled per row. Hence, the row is either "Available" or "Unavailable". Please correct me if I am wrong. 

Note that GridViewCheckBoxColumn offers the EditModeproperty. If you set it to EditMode.OnValueChange, the value will be committed to the respective cell as soon as the checkbox is toggled. Then, you should handle the RadGridView.CellValueChanged event and update the next checkbox cell. 
        private void Dgv_MenuItems_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            this.dgv_MenuItems.CellValueChanged -= Dgv_MenuItems_CellValueChanged;
            if (e.Column.Name == "Unavailable"&& (bool)e.Row.Cells["Available"].Value)
            {
                e.Row.Cells["Available"].Value = false;
            }
            else if (e.Column.Name == "Available"&& (bool)e.Row.Cells["Unavailable"].Value)
            {
                e.Row.Cells["Unavailable"].Value = false;
            }
            this.dgv_MenuItems.CellValueChanged += Dgv_MenuItems_CellValueChanged;
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Memo
Top achievements
Rank 1
Iron
Veteran
commented on 16 May 2022, 03:36 PM

Des, you're a star! 

 

Thanks for the help and have a great week!

Ian
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 01 Dec 2022, 09:28 AM | edited

This is one of those rare examples where your C# to VB converter doesn't work.

The only bit I don't understand are the two line top & bottom. Could you translate them for us poor VB'ers please? 

this.dgv_MenuItems.CellValueChanged -= Dgv_MenuItems_CellValueChanged;

and

this.dgv_MenuItems.CellValueChanged += Dgv_MenuItems_CellValueChanged;
Dess | Tech Support Engineer, Principal
Telerik team
commented on 01 Dec 2022, 09:48 AM

Hi, Ian,

AddHandler and RemoveHandler are the missing pieces for subscribing/unsubscribing from events in VB: 

        AddHandler Me.dgv_MenuItems.CellValueChanged, AddressOf Dgv_MenuItems_CellValueChanged
        RemoveHandler Me.dgv_MenuItems.CellValueChanged, AddressOf Dgv_MenuItems_CellValueChanged

Additional information can be found here: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/events/#addhandler-and-removehandler 

 

Ian
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 01 Dec 2022, 10:18 AM

Thanks - one of the few places where the C# and VB code look very different.

Perhaps you might change the C#/VB converter to understand this construct? It's a VERY useful little tool!

Dess | Tech Support Engineer, Principal
Telerik team
commented on 01 Dec 2022, 10:49 AM

Hi, Ian,

Note that we don't have control over the exact C# to VB.NET conversion as we utilize an external library for this purpose. However, it is a good tool that facilitates the conversion process for our customers. 

Tags
GridView
Asked by
Memo
Top achievements
Rank 1
Iron
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or