We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridControl: selection with the keyboard arrows

Hi, In a GridControl, I have enabled some features thanks to some help of this forum. In the grid, we have the sort function (click on the column header) and also the reorder column function. For that, I have implemented the solution found here : http://www.syncfusion.com/Support/Forums/message.aspx?MessageID=9676 The grid is also a grid where ''the whole row is selected no matter where the user clicks in the row'' (see Q579). So I have subscribed to the CurrentCellActivating event and set in the handler: e.ColIndex = 0 (focus is always on the header of the row -col 0- in my current row) Beside that, the grid is a multi-selection grid (ListBoxSelectionMode=MultiExtended). Now, I''m facing a problem. With such a configuration, I''m not able to used the keyboard arrows to select one or more rows in y grid (only the mouse can do that). I think (not sure) that it has to do with the following code myGrid.BaseStylesMap["Header"].StyleInfo.Enabled = false; So how can I have that feature and not losing all the others explained above. Thanks Mario

10 Replies

AD Administrator Syncfusion Team August 2, 2004 10:20 AM UTC

I tried to see this problem in forum sample you mentioned and could not. Below is the code I tried, adding the CurrentCellActivating event and ListBoxSelectionMode = MultiSelect as well as setting syle.Enabled = false on the Header basestyle. Are you handling any other keyboard or mouse events that might be affecting this? Or are you handling SelectionsChanging event? Forum_DragColOnFirstClick_9702.zip

MC Mario Cadelli August 2, 2004 11:24 AM UTC

Hi Clay, I just tried in the sample too and it is ok. So I have changed the code in order to match mine. So first the base grid is a GridControl instead of a Grid associated to a table. Some lines must be commented (the ones with the SortBehavior). I included in Form1_Load the code I found on the forum and in sample (to sort and move cols) private void Form1_Load(object sender, System.EventArgs e) { // Initialize some Grid Behaviour myGrid.ControllerOptions &= ~(GridControllerOptions.OleDataSource|GridControllerOptions.OleDropTarget); myGrid.BaseStylesMap["Header"].StyleInfo.Enabled = false; // The 5 next lines of code come from : Syncfusion\Essential Suite\\Grid\Samples\Quick Start\GridControlSort //turn off column selections - not absolutely needed but does simplify things myGrid.AllowSelection = GridSelectionFlags.Any & ~GridSelectionFlags.Column; //add a sort header to the CellModels... (in 1.0, only available by default in GridDataBoundGrid) myGrid.CellModels.Add("ColumnHeaderCell", new GridSortColumnHeaderCellModel(myGrid.Model)); //catch the click myGrid.CellClick += new GridCellClickEventHandler(HandleAClick); // Just fill a 10x10 grid int nCols = 10; int nRows = 10; for(int i = 1; i <=nCols; i++) for (int j=1; j<=nRows;j++) myGrid[i,j].CellValue = "Col"+i.ToString()+",Row"+j.ToString(); } Further, there is the code the manage the HandleAClick event (to do the sorting). I can''t remember where I found the 2 lines: // Initialize some Grid Behaviour myGrid.ControllerOptions &= ~(GridControllerOptions.OleDataSource|GridControllerOptions.OleDropTarget); myGrid.BaseStylesMap["Header"].StyleInfo.Enabled = false; But if I comment the second, the keyboard arrow works fine but once I start to do thing in the row header (to move columns for example), I have strange behaviors. Cell 0,0 can be selected or the whole row 0 is selected. Not really what I want. That''s what I have seen. I hope it''s clear. Mario

AD Administrator Syncfusion Team August 2, 2004 12:59 PM UTC

There are things you can do to prevent cell 0,0 and row 0 from being selected. You can remove the Table flag from the AllowSelection for example. Or, you can handle SelectionsChanging and is e.tange.IsTable or if e.Range.Intersects(GridRangeInfo.Row(0)), then set e.Cancel = true. If this does not take care of the problem you are having, can you upload your modification of the sample showing the problem?

MC Mario Cadelli August 3, 2004 03:27 AM UTC

Only the row 0 is selected only when a column move is done. Here is the sample modified with the problem. Forum_DragColOnFirstClick_3161.zip

MC Mario Cadelli August 3, 2004 03:31 AM UTC

Clay, To clarify, the zip file posted contains the original problem (keyboard arrows not working). Mario

AD Administrator Syncfusion Team August 3, 2004 06:02 AM UTC

Here is your sample back with some changes that I think give you the behavior you wanted. I think the main problem was you making the row header disabled, but then in CurrentCellActivating, you were trying to activate it. These two actions were fighting each other. The changes in teh attached sample include setting the e.ColIndex = 1 in the CurrentCellActivating, an dalso using PrepareViewStyleInfo to color teh selection instead of teh default selection drawing. This avoid sthe ''white'' currentcell that I think is why you were trying to set e.ColIndex = 0 in CurrentCellActivationg. There were a couple of othe rporpoerty setting changed to make things look a little better (at least to me). Forum_DragColOnFirstClick_2171.zip

MC Mario Cadelli August 3, 2004 09:57 AM UTC

Thanks Clay, That is almost the desired behavior. One thing is not working fine: when you select 3 rows and then change the sort order by clicking on the col B, you lose the selections. Reselect them and resort the same column. There the 3 selected rows are still there. Change the sort order from B to C, then they are lost again. Any idea what is wrong? Besides that, I have 3 extra questions. 1. As I can have images in cells, I already have code in the CellDrawn method. See attached sample modified. That code put one line out of 2 in green and also add the image in the cell. But once a line is selected, the background of the image remains to the previous color (white or green). What can I do there to paint the background of the image? 2. There is a problem with the column move. It is sometimes (almost always) triggered when a resize of a column is done. You can see that in the sample too. How can we prevent that? 3. In my application, the grid is refreshed every 5 seconds (to match data always changing). It means that the rows are deleted and recreated just after. I need also to restore the selections as they were before a refresh. When that is done, it seeems that the focus totally disappear of the grid and I would like to have the focused cell (with dashed border) on a particuliar cell (to be able to do again selection with the arrows). How can I do that? Thanks Mario Forum_DragColOnFirstClick_1433.zip

AD Administrator Syncfusion Team August 3, 2004 03:35 PM UTC

I cannot see the first problem you discussed in the sample you attached using What version are you using? 1) Remove the PrepareViewStyleInfo code and put it at the bottom of your CellDrawn event handler as an else clause on the if -statement you have handling the currentcell there.
else  if(e.ColIndex > 0 && e.RowIndex > 0 &&
this.myGrid.Selections.Ranges.AnyRangeContains(GridRangeInfo.Cell(e.RowIndex, e.ColIndex)))
  using(SolidBrush br = new
		SolidBrush(Color.FromArgb(60, myGrid.AlphaBlendSelectionColor))	)
	  e.Graphics.FillRectangle(br, e.Bounds);
2) Chnage OnMouseDown to only set inColMove when you are strictly inside teh cell and not on the borderr.
int row;
if(this.PointToRowCol(mouseDownPt, out row, out sourceCol) 
	&& row == 0 && sourceCol > 0)
	Rectangle rect = this.RangeInfoToRectangle(GridRangeInfo.Cell(row, sourceCol));
	rect.Inflate(-3, 0);
		Console.WriteLine("start move at {0}", sourceCol);
		inColMove = true;
3) You could try saving grid.CurrentCell.RowIndex and grid.CurrentCell.COlindex before the grid gets refreshed with new data. Then Afterwards, use grid.CurrentCell.MoveTo(savedRow, saveCol) to reset the currentcell.

MC Mario Cadelli August 4, 2004 05:24 AM UTC

Hi Clay, Many thanks for the help on points 1. to 3.: all working fine. About the other problem, I have version too. I check again and it only happens in my app (not on the sample). So as I need to save the current selected rows and restore them after the refresh, I put in place the same principle in the sort process. So that is now ok. Your help is really appreciated :-) Mario

TJ Tony John August 4, 2004 06:05 PM UTC

Hi Mario, I am having exactly the same requirement and I went through the sample you have posted. Can you please post your most recent version of the sample ?

Live Chat Icon For mobile
Up arrow icon