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

Hide Row and Cell selection when grid is not focused

3 Answers 587 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jaap
Top achievements
Rank 2
Jaap asked on 08 Jul 2013, 11:07 AM
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

3 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 11 Jul 2013, 08:47 AM
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 >>
0
Jaap
Top achievements
Rank 2
answered on 15 Jul 2013, 07:41 AM
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
0
Ivan Petrov
Telerik team
answered on 17 Jul 2013, 02:28 PM
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 >>
Tags
GridView
Asked by
Jaap
Top achievements
Rank 2
Answers by
Ivan Petrov
Telerik team
Jaap
Top achievements
Rank 2
Share this question
or