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

Get the right cell using GetElementAtPoint in a filtered grid

4 Answers 154 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Victor
Top achievements
Rank 2
Victor asked on 12 Sep 2016, 06:38 PM

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);


                }
               
            }

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 13 Sep 2016, 02:09 PM
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.
0
Victor
Top achievements
Rank 2
answered on 13 Sep 2016, 03:14 PM

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

0
Victor
Top achievements
Rank 2
answered on 14 Sep 2016, 07:18 AM

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

0
Hristo
Telerik team
answered on 14 Sep 2016, 03:13 PM
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.
Tags
GridView
Asked by
Victor
Top achievements
Rank 2
Answers by
Hristo
Telerik team
Victor
Top achievements
Rank 2
Share this question
or