简体   繁体   中英

hide and unhide context strip menu item c#

        if(e.Button == MouseButtons.Right)
        {
            string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
            // MessageBox.Show(signatureDate);
            if(signatureDate.Length > 5)
            {
                contextMenuStrip1.Items[0].Visible = false;
                contextMenuStrip1.Items[1].Visible = true;
            }else
            {
                contextMenuStrip1.Items[0].Visible = true;
                contextMenuStrip1.Items[1].Visible = false;
            }
        }

I have a context strip menu that is working in my datagridview. And I selected it as Row Context Strip Menu.

What I am trying to do is to get if selected row of datagridview and control signature column is null or not. If it has signature date I want to hide or unhide "Sign" and if it doesn't have signature date hide "Unsign" item on context menu strip.

You can see in picture I enclosed. Context menu Strip

EDIT: Name of the event is MouseDown. EDIT 2: With editing this code I can get columns data and show them on messageBox. But I can not use those data as a condition. Therefore it is not working. For example, when I select a row that is without "Signature Date" and show it on messageBox, it is working. But when I use Signature Date data as a condition It is not working. I know it is so strange and too easy to overcome but I coundn't because of that I didn't catch anything.

EDIT 3: Event

EDIT 4 (SOLVED) : I created to Context Strip Menu and specify no one of them as Context strip Menu of Datagridview.

With Datagridview_MouseDown event, I am getting Signature Date column data and check if it is null/empty or not. If it is null/empty I specify first Context Menu strip as Context Strip Menu of Datagridview or not I do revise. I figured out the solution in this way :)

I think your problem is in the instance of context menu strip use this one see if it helps.

if(e.Button == MouseButtons.Right)
        {
            string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
            // MessageBox.Show(signatureDate);
            if(signatureDate.Length > 5)
            {
                dataGridView3.ContextMenu.Items[0].Visible = false;
                dataGridView3.ContextMenu.Items[1].Visible = true;
            }else
            {
                dataGridView3.ContextMenu.Items[0].Visible = true;
                dataGridView3.ContextMenu.Items[1].Visible = false;
            }
        }

Probably your event is not firing. Instead of using mouse down you could also use the Opening event of the contextMenuStrip

This should solve your problem

private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView3.ContextMenu = contextMenuStrip1;
            contextMenuStrip1.Opening += contextMenuStrip1_Opening;
        }


private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            string signatureDate = dataGridView3.CurrentRow.Cells[8].Value.ToString();
            // MessageBox.Show(signatureDate);
            if (signatureDate.Length > 5)
            {
                contextMenuStrip1.Items[0].Visible = false;
                contextMenuStrip1.Items[1].Visible = true;
            }
            else
            {
                contextMenuStrip1.Items[0].Visible = true;
                contextMenuStrip1.Items[1].Visible = false;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM