VB.NET - How to add a context menu to a Gridview row using vb.net?

Asked By Radhika Jadhav on 24-Jun-09 06:12 AM
How to add a context menu to a Gridview row using vb.net?
i.e on the right click of a particular cell i want a menu like this
Suppose i have 2 option
add
n sub
and wen i click add i want 3 options say a b c..
i.e
Add-a
        b
        c
How do i do it in Gridview????
Kindly let me know asap...

Re - Ravenet Rasaiyah replied to Radhika Jadhav on 24-Jun-09 06:21 AM

Hi

Yes you can add easily

http://www.c-sharpcorner.com/UploadFile/nipuntomar/contextmenuforgridview01162007124516PM/contextmenuforgridview.aspx


thank you
http://www.codegain.com
Santhosh N replied to Radhika Jadhav on 24-Jun-09 06:24 AM

YOu could check here explaining how to do this using gridviews HitTestInfo..

http://www.c-sharpcorner.com/UploadFile/nipuntomar/contextmenuforgridview01162007124516PM/contextmenuforgridview.aspx

it says like this - Asked By Radhika Jadhav on 24-Jun-09 06:42 AM

it says  like this

//Define different context menus for different columns
private ContextMenu contextMenuForColumn1 = new ContextMenu();
private ContextMenu contextMenuForColumn2 = new ContextMenu();

Add the following line of code in the form load event:

private void Form_Load(object sender, EventArgs e)
{
    // Load all default values of controls
    populateDataGridView();

    // Add context mneu items
    contextMenuForColumn1.MenuItems.Add("Make Active", new     EventHandler(MakeActive));
    contextMenuForColumn2.MenuItems.Add(
"Delete", new     EventHandler(Delete));
    contextMenuForColumn2.MenuItems.Add(
"Register", new     EventHandler(Register));
}

Add the following code to mouseup event of the gridview:

private void dataGridView_MouseUp(object sender, MouseEventArgs e)
{
    // Load context menu on right mouse click
    DataGridView.HitTestInfo hitTestInfo;
    if (e.Button == MouseButtons.Right)
    {
        hitTestInfo = dataGridView.HitTest(e.X, e.Y);
        // If column is first column
        if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
            contextMenuForColumn1.Show(dataGridView,
new Point(e.X, e.Y));
        // If column is second column
        if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
            contextMenuForColumn2.Show(dataGridView,
new Point(e.X, e.Y));
    }
}


but i get an error saying type 'Contextmenu' not defined...


I seriously have no idea...

Kindly guide

get an error saying type 'Contextmenu' not defined... - Asked By Radhika Jadhav on 24-Jun-09 06:45 AM
it says  like this

//Define different context menus for different columns
private ContextMenu contextMenuForColumn1 = new ContextMenu();
private ContextMenu contextMenuForColumn2 = new ContextMenu();

Add the following line of code in the form load event:

private void Form_Load(object sender, EventArgs e)
{
    // Load all default values of controls
    populateDataGridView();

    // Add context mneu items
    contextMenuForColumn1.MenuItems.Add("Make Active", new     EventHandler(MakeActive));
    contextMenuForColumn2.MenuItems.Add(
"Delete", new     EventHandler(Delete));
    contextMenuForColumn2.MenuItems.Add(
"Register", new     EventHandler(Register));
}

Add the following code to mouseup event of the gridview:

private void dataGridView_MouseUp(object sender, MouseEventArgs e)
{
    // Load context menu on right mouse click
    DataGridView.HitTestInfo hitTestInfo;
    if (e.Button == MouseButtons.Right)
    {
        hitTestInfo = dataGridView.HitTest(e.X, e.Y);
        // If column is first column
        if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
            contextMenuForColumn1.Show(dataGridView,
new Point(e.X, e.Y));
        // If column is second column
        if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
            contextMenuForColumn2.Show(dataGridView,
new Point(e.X, e.Y));
    }
}


but i get an error saying type 'Contextmenu' not defined...


I seriously have no idea...

Kindly guide

Its a Web application.not a windows form - Radhika Jadhav replied to Radhika Jadhav on 24-Jun-09 06:48 AM
Its a Web application.not a windows form,
I dont think anything called contextmenu exists in web application
If it exists..Kindly let me know...
Web application - Radhika Jadhav replied to Santhosh N on 24-Jun-09 06:53 AM
Its a web application...How do i go about...
I guess the link shows it well for a windows form
Solution - Murali Mohan replied to Radhika Jadhav on 24-Jun-09 07:05 AM

Hi check this code.


private void gView_MouseUp(object sender, MouseEventArgs e)
{
    // Load context menu on right mouse click
    DataGridView.HitTestInfo info;
    if (e.Button == MouseButtons.Right)
    {
        hitTestInfo = dataGridView.HitTest(e.X, e.Y);
        // If column is first column
        if (info.Type == DataGridViewHitTestType.Cell && info.ColumnIndex == 0)
            contextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y));
        // If column is second column
        if (info.Type == DataGridViewHitTestType.Cell && info.ColumnIndex == 1)
            contextMenuForColumn2.Show(gView, new Point(e.X, e.Y));
    }
}

Santhosh N replied to Radhika Jadhav on 24-Jun-09 07:06 AM

YOu could check this one if wanted in asp.net..

http://aspalliance.com/articleViewer.aspx?aId=946&pId=-1

and this one as well..

http://msdn.microsoft.com/en-us/magazine/cc163848.aspx

Solution - Murali Mohan replied to Radhika Jadhav on 24-Jun-09 07:23 AM

Dear Radhika,

Ignore fist answer.

If web application u have to implement the javascript.

see below link.

http://luke.breuer.com/tutorial/javascript-context-menu-tutorial.htm.


How to add a context menu to a Gridview row - mv ark replied to Radhika Jadhav on 24-Jun-09 09:22 AM
This link has the javascript code for building a context menu - http://elouai.com/javascript/javascript-popup-menu.php

The default contextmenu is disabled and replaced with our own div which we hide or make visible whenever the right mouse button (the context menu) is selected.

You can adapt this for your gridview