Hi kh,
Thanks for your post.
I am not sure I understand your requirement very well. Do you want to show
the context menu based on different location the user right clicked the
mouse button? For example, when you right clicked row header or second
column, you want to display certain context menu; while for first column,
you want to display another context menu. If I misunderstand you, please
feel free to tell me, thanks!
For this, we can handle DataGridView.MouseDown event, then use
DataGridView.HitTest method to determine which cell the mouse pointer is
in. Then associate different context menu with
DataGridView.ContextMenuStrip property. Sample code like this:
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr);
}
this.dataGridView1.DataSource = dt;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hti=this.dataGridView1.HitTest(e.X, e.Y);
if (hti.ColumnIndex == -1 || hti.ColumnIndex == 1)
{
this.dataGridView1.ContextMenuStrip = this.contextMenuStrip2;
}
else
{
this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
}
}
Note: in the code snippet, I suppose that we have created 2
contextMenuStrips: contextMenuStrip1 and contextMenuStrip2.
It works well on my side. Hope it helps.
Best regards,
Terry Fei
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Chuck@newsgroup.nospam - 21 Jan 2006 02:40 GMT
I'd like to have a dataGridview like MS has in MS Access or Excel, my
users are use to that format.
When the user R Clicks on the RowHeader (left most column, it is made
visible with RowHeadersVisible property.)
The row is highlighted or if multiple rows are selected they are all
highlighted and the user would get a context menu that says:
new, cut, copy, paste, delete
Clicking one of the selections would then do the action on the row(s).
In the past getting copy to work was troublesome because of sticking
it on the clipboard as an object, getting the correct columns to copy
(hidden, autoincrement, calculated)
Pasting/copying was equally troublesome.
>Hi kh,
>Thanks for your post.
[quoted text clipped - 51 lines]
>Get Secure! - www.microsoft.com/security
>This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 24 Jan 2006 06:36 GMT
Hi Chuck,
Thanks for your feedback.
I am not sure I understand your main concern very well.
Normally, we can use HitTest to determine if the user clicked row
header(column index is -1), then select the entire row. Please refer to the
link below:
"5.11 How can I select the entire row when the user clicks on a cell in the
row?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q689q
If you want to implement copy/past function like Excel, can you give me
your requirement a details description? Such as what function you want to
get when user select copy, cut, paste.
Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Chuck P - 25 Jan 2006 14:44 GMT
If they pressed CUT it would delete the row and copy it to the clip
board.
If they pressed PASTE it would check for a valid row on the clipboard
and then insert the data at the current row
If they pressed COPY it would copy the row to the clipboard
"Jeffrey Tan[MSFT]" - 26 Jan 2006 02:51 GMT
Hi Chuck,
Thanks for your feedback.
Can you show me your key obstacle in requirement? Then I can place my
effort on the key point.
Normally, DataGridView supports unbound and bound states. In unbound state,
all the rows and cells comes from UI, so when implementing cut, copy, we
can just enumerate through all the cells in the row and get all the values
in the cell. Then we can use Clipboard.SetData to serialize the row content
in the clipboard. We have to customize the serialization process, because
there is no build in clipboard format for the row, we should place the cell
values in certain format and store in clipboard. In later paste time, we
can reparse the value(also do validation) to the clipboard value and create
a new row in the DataGridView for these values.
For bound state, the UI is controlled by the Datasource, so we should
serialize the data in the datasource, such as certain DataRow in DataTable.
Also in paste operation, we should reparse the clipboard data and add a new
DataRow to the datasource.
Hope this helps
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.