Hide Row and Cell selection when grid is not focused

4 posts, 0 answers
  1. Jaap
    Jaap avatar
    96 posts
    Member since:
    Oct 2012

    Posted 08 Jul 2013 Link to this post

    Hello,

    I have a form with multiple grids on it. I NO visualization of the selected rows and cells if the grid does not have the focus.
    I tried to use the HideSelection property, but that seems only to hide selection of selected text when a cell is in edit mode. I would expect that applies to the selected rows and cells.
    I found this tread http://www.telerik.com/community/forums/winforms/gridview/disable-cell-highlighting-row-only-selection.aspx using the row and/or cell formatting events. But that's not exactly what I want.
    Can you provide me with an example of which properties to set?

    Regards, Jaap
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 11 Jul 2013 Link to this post

    Hello Jaap,

    Thank you for writing.

    You can clear the selection in the grid when it loses focus and set the selection again when it receives focus again. Here is how to achieve this:
    this.radGridView1.LostFocus += radGridView1_LostFocus;
    this.radGridView1.GotFocus += radGridView1_GotFocus;
    this.radGridView1.CellEndEdit += radGridView1_CellEndEdit;
     
    private GridViewColumn column;
    private GridViewRowInfo row;
     
    private void radGridView1_GotFocus(object sender, EventArgs e)
    {
        if (this.column != null && this.row != null)
        {
            this.radGridView1.CurrentColumn = this.column;
            this.radGridView1.CurrentRow = this.row;
        }
    }
     
    private void radGridView1_LostFocus(object sender, EventArgs e)
    {
        if (!this.radGridView1.IsInEditMode)
        {
            this.SaveAndClearSelection();
        }
    }
     
    private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        this.SaveAndClearSelection();
    }
     
    private void SaveAndClearSelection()
    {
        this.column = this.radGridView1.CurrentColumn;
        this.row = this.radGridView1.CurrentRow;
     
        this.radGridView1.CurrentColumn = null;
        this.radGridView1.CurrentRow = null;
    }

    I hope this will be useful. Should you have further questions, I would be glad to help.

    Regards,
    Ivan Petrov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jaap
    Jaap avatar
    96 posts
    Member since:
    Oct 2012

    Posted 15 Jul 2013 Link to this post

    Hello Ivan,

    Thanks for the code. Works fine.
    But one problem with it: When opening the form, the grids with don't have the focus initially still show the current row and column.
    I can't find the correct event where I can set the CurrentColumn and CurrentRow to null after loading of the data. I tried BindingSource.BindingComplete, BindingSource.CurrentChanged and RadGridView.CurrentCellChanged, but none worked.
    Can you tell me?
    (That's why I was initially looking for solving this with something on the paint or formatting logic: if focused then paint currents else paint standard. That would be a better solution, I think, but seems not easy to do)

    Regards, Jaap
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 17 Jul 2013 Link to this post

    Hello Jaap,

    Thank you for writing.

    The Paint event is not useful in this situation, because our controls are constructed with elements and each element draws itself. So if a row is selected its element will know to draw itself as a selected row. What you can do is to call the SaveAndClearSelection method in your form constructor or in the OnLoad/OnShown methods.

    I hope this will help. Feel free to write back with further questions.

    Regards,
    Ivan Petrov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top