GridView selection mode similar to RowHeaderSelect in WinForms DataGridView

3 Answers 15 Views
GridView
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Marian asked on 21 Apr 2024, 10:33 PM | edited on 21 Apr 2024, 10:34 PM

Hello,

I am wondering if GridView really doesn't have selection mode similar to RowHeaderSelect in WinForms DataGridView, where I can select individual cells clicking on those cells or I can select row by clicking row header cell. I have read documentation and forum, and I am also wondering if somebody else really doesn't have the same question.

For example, I want to delete row only if row is selected, not if only few cells are selected in multiple rows, it's strange behavior for me. Or, second use case, I have more Copy operations depending on selection scope, if row is selected, whole data object is copied to clipboard, if individual cells are selected, just those data as text are copied, if only cells from specific columns are selected, one parameter sub-object is copied etc.

Now, I can get used to this new behavior, but users are used to DataGridView behavior, I think they will be little bit confused. Or, I know when I click to row header cell, all cells in that row are selected (also the hidden ones), so I can check if all cells in row are selected and then consider that row as selected, but it will be more processing.

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 22 Apr 2024, 06:06 PM

Hello,

I got an idea I can switch selection mode on CellClick event. I don't know if it's a good solution, but it works almost fine. I have added this to CellClick event:

private void radGridView1_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
		if (e.Row is GridViewTableHeaderRowInfo)
		{

		}
		else if (e.Column is GridViewRowHeaderColumn)
		{
			if (radGridView1.SelectionMode != GridViewSelectionMode.FullRowSelect)
			{
				radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;
				e.Row.IsSelected = true;
			}
		}
		else
		{
			if (radGridView1.SelectionMode != GridViewSelectionMode.CellSelect)
			{
				radGridView1.SelectionMode = GridViewSelectionMode.CellSelect;
				e.Row.Cells[e.ColumnIndex].IsSelected = true;
			}
		}
}

The only problem I have found so far is that when I start to select cells / rows with dragging mouse for multiselection, Click event is not fired and selection mode is not switched, as you can see at the end of animation:

Is it a good idea or do you know any disadvantages of this solution? If yes, how can I solve this problem with dragging multiselection? Maybe CellClick is not a right event, but I don't know which would be better. I have attached test project again (TestTelerikSelection).

3 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 24 Apr 2024, 02:11 PM

Hello, Marian,

You are on the right path finding out that the selection in RadGridView is controlled by the SelectionMode property. It can be set to CellSelect or FullRowSelect.

It seems to me that the behavior that you described is already available in RadGridView with SelectionMode set to CellSelect. In CellSelect mode one or more individual cells can be selected. However, when you click with the mouse on the row's header cell ( GridRowHeaderCellElement), the whole row will be selected but as you notice it will update the SelectedCells collection, not SelectedRows. This is because the mode is CellSelect and the grid counts the selected cells. This behavior is normal and comes out of the box.

If I understand you correctly, you expect to change the SelectionMode when you start dragging over the row header's cell and count the celected rows not individual cells. Am I right? If this is the case, I can suggest to create a custom GridRowBehavior to achieve this. Please refer to the following code snippet: 

public class CustomGridRowBehavior : GridRowBehavior
{
    protected override bool OnMouseDownLeft(MouseEventArgs e)
    {
		GridRowHeaderCellElement cell =this.GetCellAtPoint(e.Location) as GridRowHeaderCellElement;
        GridRowElement rowElement = this.GetRowAtPoint(e.Location);
        if (rowElement != null && cell != null)
		{
			this.GridViewElement.GridControl.SelectionMode = GridViewSelectionMode.FullRowSelect;
		}
        return base.OnMouseDownLeft(e);
    }
}

Then, you should register the new custom behavior in grid:

//register the custom row  behavior
BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridRowBehavior());

You can see the achieved result in the attached gif file. I attached the updated TelerikTestSelection project for your reference. Can you give it a try and let me know how it works for you now? Note, you can further customize this behavior according to your specific requirements. 

More information about different GridRowBehavior is available here: Row behaviors - WinForms GridView Control - Telerik UI for WinForms

I hope this helps. If you have any other questions do not hesitate to contact me. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 24 Apr 2024, 10:41 PM | edited

Hello,

thanks. What I want to achieve is RowHeaderSelect selection mode in DataGridView:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewselectionmode?view=windowsdesktop-8.0

In this mode, you can select individual cells or row. Selected row is not the same thing as all cells in that row are selected, also you have independent SelectedRows and SelectedCells properties.

I have created custom GridRowBehavior based on your sample, and it works almost perfectly.

		protected override bool OnMouseDownLeft(MouseEventArgs e)
		{
			var cell = GetCellAtPoint(e.Location);

			if (cell is GridRowHeaderCellElement)
			{
				if (GridViewElement.GridControl.SelectionMode != GridViewSelectionMode.FullRowSelect)
					GridViewElement.GridControl.SelectionMode = GridViewSelectionMode.FullRowSelect;
			}

			if (cell is GridDataCellElement)
			{
				if (GridViewElement.GridControl.SelectionMode != GridViewSelectionMode.CellSelect)
					GridViewElement.GridControl.SelectionMode = GridViewSelectionMode.CellSelect;
			}

			return base.OnMouseDownLeft(e);
		}

Now I see one more issue, that you can make selection also by keyboard. So desired behavior would be, you can stay in FullRowSelect mode and select other rows by up/down arrows, but when you press left/right arrow, control should switch back to CellSelect mode and navigate to the left or right cell. So I have overriden also ProcessLeft/RightKey like this:

		protected override bool ProcessLeftKey(KeyEventArgs keys)
		{
			if (GridViewElement.GridControl.SelectionMode != GridViewSelectionMode.CellSelect)
				GridViewElement.GridControl.SelectionMode = GridViewSelectionMode.CellSelect;

			return base.ProcessLeftKey(keys);
		}

		protected override bool ProcessRightKey(KeyEventArgs keys)
		{
			if (GridViewElement.GridControl.SelectionMode != GridViewSelectionMode.CellSelect)
				GridViewElement.GridControl.SelectionMode = GridViewSelectionMode.CellSelect;

			return base.ProcessRightKey(keys);
		}

Now it seems to be perfect. I tried also scenarios how it behaves for example in edit mode, and I haven't found any problem so far. I have attached also animation, I think it's ok. Do you know about something what else to test, or do you know about any other scenarios, which are not handled by this?

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 24 Apr 2024, 11:08 PM

Sorry, one more comment. I added the same code to the real test application and it doesn't work there. I have checked it all, code is the same, registration is the same, I don't understand, why it doesn't work there? CustomGridRowBehavior.OnMouseDownLeft method is not even called, ProcessLeft/RightKey neither. I have attached modified test project, it works in TelerikTestSelection, but not in TelerikTestReal.
0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Apr 2024, 12:57 PM

Hello, Marian,

The provided methods OnMouseDownLeft , also ProcessLeftKey and ProcessRightKey seem ok to me. I am glad to hear that they are working perfectly now. Note, RadGridView provides the customizing row behavior feature and these public methods to allow our customers to cover different user cases. However, the exact implementation and which scenarios to handle is up to the developer. Feel free to modify or extend them in the most suitable manner for you any time when you decide to cover different scenarios. 

I noticed that your TelerikTestReal project uses RadGridView in hierarchy mode, while in the TelerikTestSelection the grid is not in hierarchy mode. When RadGridView is in hierarchy, it uses GridHierarchyRowBehavior and GridViewHierarchyRowInfos. Hence, you should create a custom GridHierarchyRowBehavior, and register it for GridViewHierarchyRowInfo:

public class CustomGridRowBehavior : GridHierarchyRowBehavior
{
// custom logic here
}

//register the custom hierarchy behavior
var gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
gridBehavior.UnregisterBehavior(typeof(GridViewHierarchyRowInfo));
gridBehavior.RegisterBehavior(typeof(GridViewHierarchyRowInfo), new CustomGridRowBehavior());

This will register the custom hierarchy behavior to the grid, and you should be able to get it work in desired way. 

If you have any other questions do not hesitate to contact me.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 26 Apr 2024, 05:26 PM

Hello,

thank you very much. I have seen GridViewHierarchyRowInfo in documentation and also tried it, maybe I made some mistake somewhere, but it works now. Just one more detail in behavior, when I edit some cell, and click to other cell, or navigate by keyboard, it begins edit also in new cell. I think it would be more natural to not begin edit in new cell, this behavior is in WinForms DataGridView and I think it's little bit annoying to press escape after navigating to other cell. If this can be also customized, it would be great, but it's not a problem, just a wish.

0
Nadya | Tech Support Engineer
Telerik team
answered on 29 Apr 2024, 03:26 PM

Hello, Marian,

I am glad that the suggested solution works for you.

According to the editing behavior, the behavior that you just described is default one. More information regarding this is available here: Editing Behavior - WinForms GridView Control - Telerik UI for WinForms

In order to change the default functionality in RadGridView according to your custom criteria, you can set the RadGridViewBeginEditMode property to BeginEditProgrammatically and activate the editor programmatically in a way that is most suitable for you. For example in the CellDoubleClick event. Here is a sample code snippet: 

 this.radGridView1.BeginEditMode = Telerik.WinControls.RadGridViewBeginEditMode.BeginEditProgrammatically;
 this.radGridView1.CellDoubleClick+=radGridView1_CellDoubleClick;

private void radGridView1_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    this.radGridView1.BeginEdit();
}

In this way, the cell will enter into edit mode when you double click over some cell but will not get the next cell into edit mode when you click with the mouse or use the keystrokes. 

I hope this helps. Should you have any other questions please let me know.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Tags
GridView
Asked by
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or