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

Context Menu

Hi, I am trying to pop up context menu on the column header and cells of the particular column header of GridDataBoundGrid. On mouse up event i am getting row and column using following method m_GridDataBoundGrid1.PointToRowCol(new Point(e.X,e.Y), out m_LastRow, out m_LastCol); On the column headers context menu is coming properly but on the right click of the cell it doesnt come up. My requirement is to select the cell by left click and show context menu on the right click (Context menu appears when the cell is made deactive) Can you help?

5 Replies

AD Administrator Syncfusion Team December 2, 2004 08:23 AM UTC

The problem is that when a cell is active, the grid does not get the mouse up. Instead the cell control gets it. So, one way to handle this is to subcribe to the cell control''s mouseup event. You can do this dynamically as you move from cell to cell in teh grid''s CurrentCellControlGotFocus event. You would also need to unsubscribe to it in the LostFocus event. Here are some snippets.
private void gridDataBoundGrid1_MouseUp(object sender, MouseEventArgs e)
{
	int row, col;
	if(e.Button == MouseButtons.Right
		&& this.gridDataBoundGrid1.PointToRowCol(new Point(e.X,e.Y), 
		out row, 
		out col))
	{
		ShowMenuFor(row, col);
	}
}
private void currentCell_MouseUp(object sender, MouseEventArgs e)
{
	if(e.Button == MouseButtons.Right)
	{
		GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
		ShowMenuFor(cc.RowIndex, cc.ColIndex);
	}
}
private void gridDataBoundGrid1_CurrentCellControlGotFocus(object sender, ControlEventArgs e)
{
	e.Control.MouseUp += new MouseEventHandler(currentCell_MouseUp);
}
private void gridDataBoundGrid1_CurrentCellControlLostFocus(object sender, ControlEventArgs e)
{
	e.Control.MouseUp -= new MouseEventHandler(currentCell_MouseUp);
}
private void ShowMenuFor(int row, int col)
{
	MessageBox.Show(string.Format("menu for ({0},{1}", row, col));
}


AD Administrator Syncfusion Team December 2, 2004 10:26 AM UTC

Thanks for the reply But there is not current cell mouse up event for DataBoundGrid when i specified cell mouse event i got following compilation error SlbWFTIPretestListingUI.m_GridDataBoundGrid1_CellMouseUp(object, Syncfusion.Windows.Forms.Grid.GridCellMouseEventArgs)'' does not match delegate ''void System.Windows.Forms.MouseEventHandler(object, System.Windows.Forms.MouseEventArgs)'' Can you help? >The problem is that when a cell is active, the grid does not get the mouse up. Instead the cell control gets it. So, one way to handle this is to subcribe to the cell control''s mouseup event. You can do this dynamically as you move from cell to cell in teh grid''s CurrentCellControlGotFocus event. You would also need to unsubscribe to it in the LostFocus event. Here are some snippets. >
>private void gridDataBoundGrid1_MouseUp(object sender, MouseEventArgs e)
>{
>	int row, col;
>	if(e.Button == MouseButtons.Right
>		&& this.gridDataBoundGrid1.PointToRowCol(new Point(e.X,e.Y), 
>		out row, 
>		out col))
>	{
>		ShowMenuFor(row, col);
>	}
>}
>private void currentCell_MouseUp(object sender, MouseEventArgs e)
>{
>	if(e.Button == MouseButtons.Right)
>	{
>		GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
>		ShowMenuFor(cc.RowIndex, cc.ColIndex);
>	}
>}
>private void gridDataBoundGrid1_CurrentCellControlGotFocus(object sender, ControlEventArgs e)
>{
>	e.Control.MouseUp += new MouseEventHandler(currentCell_MouseUp);
>}
>private void gridDataBoundGrid1_CurrentCellControlLostFocus(object sender, ControlEventArgs e)
>{
>	e.Control.MouseUp -= new MouseEventHandler(currentCell_MouseUp);
>}
>private void ShowMenuFor(int row, int col)
>{
>	MessageBox.Show(string.Format("menu for ({0},{1}", row, col));
>}
>


AD Administrator Syncfusion Team December 2, 2004 11:48 AM UTC

The currentCell_MouseUp method is defined in the above code. It is not a member of anything except the form where it resides. It is the delegate that will be used to handle the active cell control''s MouseUp event. So, I made its signature to be that of a Control.MouseUp event handler so it can be used as a Control.MouseUp handler. Here is the working sample from where I copied the above code. WindowsApplication11_1762.zip


AD Administrator Syncfusion Team December 3, 2004 12:15 AM UTC

It worked. In sample attached,there is one extra row created .Can you tell the reason and how can we avoid creating that extra row? i have the same issue of extra row in my application Secondly, i have requirement of Multi-selection of cells by left click with the control key held (same as Excel) or click and drag. Is there any property for that? >The currentCell_MouseUp method is defined in the above code. It is not a member of anything except the form where it resides. It is the delegate that will be used to handle the active cell control''s MouseUp event. So, I made its signature to be that of a Control.MouseUp event handler so it can be used as a Control.MouseUp handler. > >Here is the working sample from where I copied the above code. > >WindowsApplication11_1762.zip > >


AD Administrator Syncfusion Team December 3, 2004 08:24 AM UTC

That is the AddNew row where you users can enter new a record if you want them to be able to add new records in this manner. It is an MS-Access type feature (it is part of the Windows Forms DataGrid also.) If you do not want it, set this.gridDataBoundGrid1.EnableAddNew = false;.

Loader.
Up arrow icon