logo
down
shadow

How to show different contextmenustrip in datagridview using c#


How to show different contextmenustrip in datagridview using c#

By : Ste Porritt
Date : November 18 2020, 03:49 PM
it fixes the issue Simply use the MouseUp event to detect the mouse click. The DataGridView.HitTest() method can tell you what part of the DGV was clicked, allowing you to pick the CMS you want. For example:
code :
    private void dataGridView1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button != MouseButtons.Right) return;
        var dgv = (DataGridView)sender;
        ContextMenuStrip cms = null;
        var hit = dgv.HitTest(e.X, e.Y);
        switch (hit.Type) {
            case DataGridViewHitTestType.ColumnHeader: cms = contextMenuStrip1; break;
            case DataGridViewHitTestType.Cell: cms = contextMenuStrip2; break;
        }
        if (cms != null) cms.Show(dgv, e.Location);
    }


Share : facebook icon twitter icon
In Winforms, how do you pass the datagridview row right-clicked on to the ContextMenuStrip?

In Winforms, how do you pass the datagridview row right-clicked on to the ContextMenuStrip?


By : user1794946
Date : March 29 2020, 07:55 AM
Does that help I need to know which row is right-clicked on for the contextMenuStrip in a datagridview. Winforms, c#.
code :
int id = (int)YourGridViewName.SelectedCells[0].Value;
C# - DatagridView and ContextMenuStrip

C# - DatagridView and ContextMenuStrip


By : user3328359
Date : March 29 2020, 07:55 AM
like below fixes the issue It looks like you want to open your ContextMenuStrip if your user right clicks the header of your DataGridView's last column. I would use the DataGridView MouseDown event and in that event check for these conditions and if they're met call the Show method of your ContextMenuStrip.
Like this:
code :
private void dataGridView1_MouseDown(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Right) {
        var ht = dataGridView1.HitTest(e.X, e.Y);
        // See if the user right-clicked over the header of the last column.
        if ((    ht.ColumnIndex == dataGridView1.Columns.Count - 1) 
             && (ht.Type == DataGridViewHitTestType.ColumnHeader)) {
            // This positions the menu at the mouse's location.
            contextMenuStrip1.Show(MousePosition);
        }
    }
}
vb.net DataGridview show custom ContextMenuStrip on cell that is being edited

vb.net DataGridview show custom ContextMenuStrip on cell that is being edited


By : user3548796
Date : March 29 2020, 07:55 AM
With these it helps I'm trying to show a custom contextmenustrip on my datagridview and it works fine except when the cell is being being edited. Then it shows the default windows contextmenustrip with copy/cut/...
code :
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, _
                    ByVal e As DataGridViewEditingControlShowingEventArgs) _
                    Handles DataGridView1.EditingControlShowing
       If TypeOf e.Control Is TextBox Then
          With DirectCast(e.Control, TextBox)
             .ContextMenuStrip = ContextMenuStrip2
          End With
       End If
End Sub
Disable Right-Click for ContextMenuStrip in DataGridView

Disable Right-Click for ContextMenuStrip in DataGridView


By : the_birdman
Date : March 29 2020, 07:55 AM
wish helps you I have a DataGridView with a dgv1.CellClick.
code :
    private MouseButtons e_Button = new MouseButtons();
    private void dgv1_MouseDown(object sender, MouseEventArgs e)
    {
        e_Button = e.Button;
    }

    private void cms1_Opening(object sender, CancelEventArgs e)
    {
        if (e_Button == System.Windows.Forms.MouseButtons.Right)
            e.Cancel = true;
    }
datagridview contextmenustrip : showing only for chosen rows?

datagridview contextmenustrip : showing only for chosen rows?


By : minhajrezvi1
Date : March 29 2020, 07:55 AM
hope this fix your issue Sounds to me like you want to open your ContextMenuStrip if your user right clicks the header of your DataGridView's column that satisfies some condition.
In short: use the DataGridView MouseDown event and in that event check for the conditions and if they're met call the Show method of your ContextMenuStrip.
code :
private void dataGridView1_MouseDown(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Right) {
        var ht = dataGridView1.HitTest(e.X, e.Y);
        // Place your condition HERE !!!
        // Currently it allow right click on last column only
        if ((    ht.ColumnIndex == dataGridView1.Columns.Count - 1) 
             && (ht.Type == DataGridViewHitTestType.ColumnHeader)) {
            // This positions the menu at the mouse's location
            contextMenuStrip1.Show(MousePosition);
        }
    }
}
Related Posts Related Posts :
  • Can we create and access a registry key in HKEY_LOCAL_MACHINE without running application with admin permissions in C#
  • Control light intensity with a UI slider
  • Threading (Does these threads exits or aborts itself?)
  • Change the function of a button if Logged in
  • how to accept value in date format in textbox in C# windows form?
  • remove the lines from RichtextBox?
  • How to call String.Split that takes string as separator?
  • Specify task timeout in parallel linq to objects
  • comparing two custom objects to prevent duplicates
  • How to Print Text from ComboBox into a MessageBox
  • How can I do a Func<object[],Expression<Func<T,bool>>> dynamic?
  • C# SqlDataReader No data exists for the row/column
  • My code is not giving the desired output
  • Is it possible to have multiple MVC routes point to the same controller/view?
  • Updating Listbox results in " Invalid cross-thread access."
  • Finding Elbow Angle with Kinect
  • Host a mvc web api application in the sub-folder of a website
  • How to generate a sequential unique id in a thread-safe way
  • How to add a separator to a WPF combobox that is databound?
  • optimize linq query with related entities
  • Optimize this code for large input
  • Is there any way to clear all list box short way?
  • ACR122 Device Programming sample does not find reader
  • Parallel Library: does a delay on one degree of parallelism delay all of them?
  • How to convert a var which contains Long to an Long[]
  • Quickly prune and create valid data combinations
  • Get 3 parameters out of a Dictionary<string, Dictionary<string Action>>
  • Regex IsMatch taking too long to execute
  • Shuffling divs using C# behindcode
  • ASP.NET Project and IE10 Compatibility Mode issues in Windows 7
  • Why can't I loop through this dictionary?
  • RavenDB Stream for Unbounded Results - Connection Resilience
  • How to remove the focus border of a CheckBox in C# Visual Studio?
  • Exception while using String.Format "Index (zero based) must be greater than or equal to zero and less than the siz
  • Caching an LDAP connection
  • C# How to detect kill event sender
  • How to Display Splashscreen and login form at same time using c#
  • Read .sql script with parameters
  • Why would I need to compile/generate methods/code at runtime?
  • Unable to create a constant value of type 'Project.Models.Subcategory'. Only primitive types or enumeration types are su
  • How to run possibly synchronous or possibly asynchronous code?
  • Drop SQLite tables using list of tables names
  • Can I organize Git submodules in a flat hierarchy?
  • How to make a server reply after receiving a request/data
  • Stored Procedure works in DB but not in C# code
  • MVVM WPF Programmatically add Child Elements
  • NCrunch and PostSharp Logging clash
  • Increasing memory usage in socket client
  • Multiple DropDownList but same action
  • How to get application memory usage as shown in Task Manager?
  • Exposing interfaces instead of concrete classes
  • How to get only postal_code data from google api?
  • Is there a way to get rid of the (by dot42) using community licence?
  • Why must I implement a function for an interface in vb.net which apparently doesn't need to be implemented in C#
  • How to remove HTML tags from the gridview Row
  • Get public post from Facebook's Graph Search API 2.x with Facebook SDK for .NET
  • C# DLL Export to Inno-Setup - Error E0434F4D
  • syntax error insert into database
  • By-pass else block of int.TryParse in Lambda Syntax
  • Retrieve "property getter" from the property name
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org