Search code examples
c#radgridview

Show selected row in gridview


I have a data entry form with a RadGridView. The data is indexed alphabetically in the main field of the row. When I add a new row and press update, off course it goes to its place in the row order. But I don't only want that the new row will be selected, but i also want that my grid window will be shown. When I was using the default GridView in C# this code worked perfectly:

private void btnUpdate_Click(object sender, EventArgs e)
{
    int id = 0;            
    id = Convert.ToInt32(lblId.Text);               
    foreach (var dgr in gridViewCompanies.Rows)
    {
        if (dgr.Cells[0].Value.Equals(tbl.ID))
        {
            dgr.IsSelected = true;
            dgr.Cells[0].IsSelected = true;
            gridViewCompanies.FirstRowIndex = dgr.Index;
            break;
        }
    }
 }

(I just copied this related part of the code)

But now that I am using RadGridView (for some other reason) that last line gridViewCompanies.FirstRowIndex = dgr.Index; seems not to be working. That means my new row is selected but the view won't show it. I have to scroll down to find the newly added row.


Solution

  • This is what finally worked:

    private void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = 0;            
            id = Convert.ToInt32(lblId.Text);               
            foreach (var dgr in gridViewCompanies.Rows)
            {
                if (dgr.Cells[0].Value.Equals(tbl.ID))
                {
                    dgr.IsSelected = true;
                    dgr.Cells[0].IsSelected = true;GridTableElement tableElement = this.gridViewCompanies.CurrentView as GridTableElement;
                            tableElement.ScrollToRow(dgr.Index);
    
                    break;
                }
            }
         }