Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display a context menu for a grid ?

Status
Not open for further replies.

TerDavis

Technical User
Sep 18, 2002
36
0
0
US
Hello,

I created a context menu, and set the context menu property for a grid to testContextmenu.
However, when the grid has focus it always brings up the default context menu, undo, cut, copy ...etc. instead of the one i defined....

Initially, the grid is empty and does not have any values, only one blank row...so why doesn't my context menu display on a right mouse click ??
 
Are you right clicking inside the TextBox during an edit when there is a blinking cursor or are you right clicking on something like the row headers or blank part of the grid where there are no rows?

The DataGrid uses child controls for edits and more than likely the context menu you set to the DataGrid does not apply to those child controls.
 
When a cell in the grid has the focus then the context menu of the DataGridTextBox control is shown which is not the one you set for the grid.
The DataGridTextBox and the DataGrid are inherit the ContextMenu property from the Control class.
You add the Contextmenu from the grid at design time or run time:
Code:
m_DataGrid1.ContextMenu = contextMenu1;
private void ShowContextMenuForGrid()
{
		
	contextMenu1.MenuItems.Clear();
	contextMenu1.MenuItems.Add("Details",new System.EventHandler(this.contextMenu1_Popup));
	//..
}
private void contextMenu1_Popup(object sender, System.EventArgs e)
{

}

When you right click on the grid but when the focus is not on any cell then "Details" is shown.
To change the context menu for cells use a TableStyle object and set there the ContextMenu property for each cell.
Example:
Code:
m_DataGrid.TableStyles.Clear(); 
DataGridTableStyle vStyle = new DataGridTableStyle();
vStyle.AlternatingBackColor = System.Drawing.Color.Bisque;
vStyle.RowHeadersVisible = false;
vStyle.MappingName = "Tablename";
foreach(DataColumn vColumn in m_DataSet.Tables[0].Columns)
{
 	DataGridTextBoxColumn vColumnStyle= new DataGridTextBoxColumn();
 	// Set here column style
	vColumnStyle.HeaderText = "ColumnName";
	vColumnStyle.MappingName = "ColumnName" ;
	vColumnStyle.ReadOnly =true;
	vColumnStyle.Width = 120;
	// create and set context menu for this DataColumn
	ContextMenu contextMenu2 = new ContextMenu();
	contextMenu2.Add("MyFirstNameOptions", new System.EventHandler(this.contextMenu2_Popup)
	DataGridTextBox myGridTextBox = (DataGridTextBox) vColumnStyle.TextBox;
	myGridTextBox.ContextMenu=contextMenu2;
	m_DataGrid.TableStyles.Add(vStyle); 
}
private void contextMenu2_Popup(object sender, System.EventArgs e)
{

}
As you can see, you could have a different context menu for each column.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top