Get the right cell using GetElementAtPoint in a filtered grid

5 posts, 0 answers
  1. Victor
    Victor avatar
    11 posts
    Member since:
    Aug 2016

    Posted 12 Sep Link to this post

    Hi,

    I am using the code below to drag the value of a specific field in a grid towards another grid and it works fine, until the moment that I filter the grid. Then this function returns the rowindex on the screen and not the actual rowindex in the grid.

    How do I get the 'real' rownumber in order to drag the right data?

     

    Thanks.

     

           private void gridTrekker_MouseDown_1(object sender, MouseEventArgs e)
            {
                var currentCell = gridTrekker.ElementTree.GetElementAtPoint(gridTrekker.PointToClient(Cursor.Position)) as GridDataCellElement;
                if (currentCell != null)
                {
                    var rowIndex = currentCell.RowIndex;
                    if (rowIndex >= 0)
                    {
                        this.gridTrekker.CurrentRow = this.gridTrekker.Rows[rowIndex];
                        this.gridTrekker.CurrentRow.IsSelected = true;
                        gridTrekker.DoDragDrop("T-" + gridTrekker.CurrentRow.Cells["Kenteken"].Value, DragDropEffects.Copy);


                    }
                   
                }

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 13 Sep Link to this post

    Hi Victor,

    Thank you for writing.

    Accessing the indices in this way will result in the experienced behavior because the item count of the filtered data view is different than the one without filters. You can get the index of the row this way: 
    int index = currentCell.RowInfo.ViewTemplate.Rows.IndexOf(currentCell.RowInfo);

    Alternatively, I can suggest handling the CellClick event of the grid. The event arguments provide information on the row index:
    private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
    {
        GridDataCellElement currentCell = this.radGridView1.ElementTree.GetElementAtPoint(this.radGridView1.PointToClient(Cursor.Position)) as GridDataCellElement;
        if (currentCell != null)
        {
            var rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                //...
            }
        }
    }

    I hope this helps. Should you have further questions do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Victor
    Victor avatar
    11 posts
    Member since:
    Aug 2016

    Posted 13 Sep Link to this post

    Thank you very much Hristo.

    In this case I will use your first solution, as this is on a MouseDown event at the start of a dragdrop.

    Kind regards

    Victor

  5. Victor
    Victor avatar
    11 posts
    Member since:
    Aug 2016

    Posted 14 Sep in reply to Hristo Merdjanov Link to this post

    Hello Hristo,

    One more question :

    I am using a grid with a master-detail record. If I drop my item on 'radGridView1' rowIndex returns the rowIndex of either the master table-row or the detail table row. So far so good, but how can I discern, - I guess out of MouseEventArgs - whether the drop has taken place on the master grid-row or detail grid-row;

     

    Thanks in advance

    Victor

  6. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 14 Sep Link to this post

    Hi Victor,

    Thank you for writing.

    You can call the HasChildRows method of the row info associated with the cell. Please also note that in order to get the correct element you should pass the point received as an event argument to the GetElementAtPoint method: 
    private void radGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        GridDataCellElement currentCell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
        if (currentCell != null)
        {
            if (currentCell.RowInfo.HasChildRows())
            {
                //...
            }
        }
    }

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready