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

Get parent grid of row or cell

2 Answers 1072 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 07 Oct 2011, 04:20 PM
I can't seem to find how to get the parent grid of a cell or row.  Is this possible? How?

Why?
I found a decent way to be able to deselect rows while maintaining the ability to shift select a range of rows (can't do that with any of the default modes).

I don't want to hard code the grid name below (myGrid in this case):

EventManager.RegisterClassHandler(typeof(GridViewCell), MouseDownEvent, new MouseButtonEventHandler(GridViewRowOnMouseRightButtonDown));
 
void GridViewRowOnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (myGrid.SelectedItems.Count == 1)
            {
                GridViewCell cell = sender as GridViewCell;
                GridViewRow cellRow = cell.ParentRow as GridViewRow;
                if (cellRow.IsSelected && cellRow.IsValid)
                {
                    cellRow.IsSelected = false;
                    e.Handled = true;
                }
                else
                {
                    return;
                }
            }
        }

2 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 07 Oct 2011, 06:23 PM
Hi Brad,

 

Why not use ParentOfType<>() extension method instead? You may do something like the following:


var cell = sender as GridViewCell;
var row = cell.ParentOfType<GridViewRow>();


Kind regards,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Binshidha
Top achievements
Rank 1
answered on 19 Aug 2020, 08:36 AM

Hi,

I am converting an existing controls to telerik control.

I am using some custoe methods, I need to convert the it also related with this change.

DataGridViewCell was used in old case, I didn' see GridViewCell in telerik and I tried to use GridViewCellInfo, but I couldn't convert GridViewCellInfo to some required formats like GridViewComboBoxCell, GridViewCheckBoxCell.

Sub SetupCellValue(ByVal cellData As DataGridViewCell, ByVal value As String)
            If (Not CStr(cellData.Value) = value) Then
                If (TypeOf cellData Is DataGridViewComboBoxCell) Then

                    Dim comboCell As DataGridViewComboBoxCell = CType(cellData, DataGridViewComboBoxCell)

                    'See if this is valid
                    Dim valid As Boolean = False
                    For Each item As String In comboCell.Items
                        If (item = value) Then
                            valid = True
                        End If
                    Next

                    'If not valid nulify it and set the null value
                    If (Not valid) Then
                        If (CStr(cellData.Style.NullValue) <> value) Then cellData.Style.NullValue = value
                        cellData.Value = Nothing
                    Else
                        cellData.Value = value
                    End If

                ElseIf (TypeOf cellData Is DataGridViewCheckBoxCell) Then
                    'If its a check box ensure it converts correctly to a boolean
                    Dim result As Boolean = False
                    Boolean.TryParse(value, result)
                    If cellData.DataGridView.CurrentCell Is Nothing Then
cellData.DataGridView.CurrentCell = cellData.DataGridView.Rows(0).Cells(0)
                    End If
                    Dim doStrangeThing As Boolean = Not cellData.ReadOnly
                    If doStrangeThing Then cellData.DataGridView.BeginEdit(True)
                    cellData.Value = result
                    If doStrangeThing Then cellData.DataGridView.EndEdit()                  
               End If
            End If
        End Sub

 

This is the method that I need to convert in the way it supports telerik.

I tried to use GridCellElement instead of DataGridViewCell,GridComboBoxCellElement instead of DataGridViewComboBoxCell,GridCheckBoxCellElement instead of DataGridViewCheckBoxCell. will it work properly. Also i didnt get property like cellData.DataGridView.CurrentCell for GridCheckBoxCellElement 

 

 

Tags
GridView
Asked by
Brad
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Binshidha
Top achievements
Rank 1
Share this question
or