Getting cell and item when cell is clicked

2 Answers 49 Views
GridView
Neil N
Top achievements
Rank 2
Iron
Iron
Veteran
Neil N asked on 01 Jul 2024, 02:45 PM

Say I have a RadGridView bound to this class:

    public class UserActivityNoteDTO
    {
        public Guid ActivityNoteId { get; set; }
        public string ActivityType { get; set; }
        public string ActivityStage { get; set; }
        public string EntityId { get; set; }
        public string MemberId { get; set; }

   }

When a cell is clicked I need to get the ActivityNoteId and what was clicked and the value.  So if I click a member id, I need to get the ActivityNoteId and MemberId and know MemberId was clicked (and not EntityId).

I've seen examples where you can get the rowitem or cell value but not both.

2 Answers, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 04 Jul 2024, 08:42 AM

Hello Neil,

Both the GridViewCell (element representing the cells in RadGridView) and the GridViewRow (element representing the rows in RadGridView) will have the same value for their DataContext property, which will be an instance of the UserActivityNoteDTO object. To distinguish, which cell has been clicked, you could utilize the PreviewMouseLeftButtonDown event of RadGridView and via the event arguments, retrieve the GridViewCell element. The ParentOfType extension method could be used to easily traverse the visual tree. To see, which cell for what property has been clicked, you could check the Column property of the retrieved GridViewCell, which will provide information from its DataMemberBinding property for the property that it represents of the UserActivityNoteDTO class.

With this approach, both the instance of the UserActivityNoteDTO class and, which GridViewCell has been clicked on (and information about it), can be retrieved.

On a side note, since the UI elements of RadGridView will be retrieved, it will be best to not modify their properties directly, as it will result in unwanted visual behaviors due to the UI virtualization logic of the control.

Could you give this suggestion a try?

Regards,
Stenly
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.

0
Neil N
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 04 Jul 2024, 08:29 PM

I got this code working:


    private void GridView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Find the original source of the event
        var source = e.OriginalSource as DependencyObject;

        // Walk up the visual tree to find the cell that was clicked
        while (source != null && !(source is GridViewCell))
        {
            source = VisualTreeHelper.GetParent(source);
        }

        if (source is GridViewCell cell)
        {
            // Get the column of the clicked cell to determine what was clicked
            var column = cell.Column as GridViewDataColumn;

            if (column != null && !string.IsNullOrEmpty(column.UniqueName) && (column.UniqueName == "MemberId" || column.UniqueName == "MemberName" || column.UniqueName == "Attachments"))
            {
                // Get the data item for the row where the click occurred
                var dataItem = cell.ParentRow.Item as UserActivityNoteDTO;

                if (dataItem == null) return;

                // Get the value of the clicked cell
                var cellValue = cell.Value;

                if (column.UniqueName == "MemberId" || column.UniqueName == "MemberName")
                {
                    var command = ((UserActivityPageViewModel)DataContext).MemberIdClickedCommand;
                    if (command != null && command.CanExecute(dataItem))
                    {
                        command.Execute(dataItem);
                    }
                }
                else if (column.UniqueName == "Attachments")
                {
                    var command = ((UserActivityPageViewModel)DataContext).AttachmentClickedCommand;
                    if (command != null && command.CanExecute(dataItem))
                    {
                        command.Execute(dataItem);
                    }
                }
            }
        }
    }

Tags
GridView
Asked by
Neil N
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Stenly
Telerik team
Neil N
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or