Solved

Visual C#, Get selected text from current cell in DataGridView, bye pass default context menu

Posted on 2009-05-11
6
2,339 Views
Last Modified: 2013-12-17
Hi Friends,

I am Developing a Windows Application in VS 2008 using C#. I am using DataGridView to allow users to add comments and instructions in multiple rows. and I want to know;

1.  Is there any way to get selected text from current cell,  e.g.  "how are you doing" is a given text in a cell and user selected only word "doing". how to retrieve it programmatically.

2. how can we get the complete word on which click is made by user, e.g. "how are you doing" is a text in cell and user click anywhere/any character on/of "doing"  and we get the word "doing" as result.

3. How we can bye-pass default context menu which appears on Right click when Cell is in edit mode, i want to replace that menu with my own options.

thanks in advance,
Arun Vashist

0
Comment
Question by:ArunVashist
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 24357061
you might need to mod this for your purposes, but it should point you in the right direction.
    public partial class Form1 : Form

    {

        DataGridViewTextBoxEditingControl editingControl = null;

        public Form1()

        {

            InitializeComponent();

        }
 

        private void button2_Click(object sender, EventArgs e)

        {

            MessageBox.Show(editingControl.SelectedText);

        }
 

        private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

        {

            if (e.Control is DataGridViewTextBoxEditingControl)

            {

                editingControl = (DataGridViewTextBoxEditingControl)e.Control;

            }

        }

    }

Open in new window

0
 

Author Comment

by:ArunVashist
ID: 24357615
Hi RPCIT,

Thanks for your response,
point 1-2 is already fixed, can you guide me about point 3, context menu.

thanks,
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24358423
i believe you can do this by simply... (see code)

You will obviously need to actually create the contect menu, and wire it's events..

        private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

        {

            if (e.Control is DataGridViewTextBoxEditingControl)

            {

                editingControl = (DataGridViewTextBoxEditingControl)e.Control;

                editingControl.ContextMenu = new ContextMenu(.....);

            }

        }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ArunVashist
ID: 24362385
Hi RPCIT,

it work, now I am able to attach customize context menu, but again it leads to another issue. The context menu attached is working fine I can now add/remove items in it but the issue here is Right Click event is not being raised anymore.

When I am in Editmode of DataGridView Cell the below given function "DiagIns_CellMouseClick" raises on Left Click only but Right Click never raises this event,

I want to add few more items to context menu when user click right button of mouse, but right click never happen to call this function. please guide.



private void dgDiagIns_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].IsInEditMode == true)
            {              

                if (e.Button == MouseButtons.Right)
                {
                    DataGridViewCell cell = dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"];
                    dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].ContextMenuStrip = new ContextMenuStrip();
                    TextBox tb = (DataGridViewTextBoxEditingControl)CurEditControl;
                   
                    int ClickIndex = tb.GetCharIndexFromPosition(e.Location);
                    int LastIndex = tb.Text.LastIndexOf(" ", ClickIndex) + 1;
                    if(tb.Text != "")
                    tb.Text += " ";
                    int NextIndex = tb.Text.IndexOf(" ", ClickIndex);

                    if ((NextIndex - LastIndex) > 0)
                    {

                        string eval = tb.Text.Substring(LastIndex, NextIndex - LastIndex);
                        cmsIcdCodes.Items.Clear();
                        cmsIcdCodes.Items.Add(eval + " 1");
                        cmsIcdCodes.Items.Add(eval + " 2");
                        cmsIcdCodes.Items.Add(eval + " 3");
                        cmsIcdCodes.Items.Add(eval + " 4");
                        cmsIcdCodes.Items.Add(eval + " 5");
                        //cmsIcdCodes.Show(CurEditControl, e.Location);
                        CurEditControl.ContextMenuStrip = cmsIcdCodes;
                    }
               
                }

            }

           
        }


private void dgDiagIns_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            this.CurEditControl = e.Control;
            cmsIcdCodes.Items.Clear();
            cmsIcdCodes.Items.Add("Add new here");
            CurEditControl.ContextMenuStrip = cmsIcdCodes;          
           
        }

        private void dgDiagIns_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].IsInEditMode == true)
            {              

                if (e.Button == MouseButtons.Left)
                {
                    DataGridViewCell cell = dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"];
                    dgDiagIns.Rows[e.RowIndex].Cells["Diagnosis"].ContextMenuStrip = new ContextMenuStrip();
                    TextBox tb = (DataGridViewTextBoxEditingControl)CurEditControl;
                   
                    int ClickIndex = tb.GetCharIndexFromPosition(e.Location);
                    int LastIndex = tb.Text.LastIndexOf(" ", ClickIndex) + 1;
                    if(tb.Text != "")
                    tb.Text += " ";
                    int NextIndex = tb.Text.IndexOf(" ", ClickIndex);

                    if ((NextIndex - LastIndex) > 0)
                    {

                        string eval = tb.Text.Substring(LastIndex, NextIndex - LastIndex);
                        cmsIcdCodes.Items.Clear();
                        cmsIcdCodes.Items.Add(eval + " 1");
                        cmsIcdCodes.Items.Add(eval + " 2");
                        cmsIcdCodes.Items.Add(eval + " 3");
                        cmsIcdCodes.Items.Add(eval + " 4");
                        cmsIcdCodes.Items.Add(eval + " 5");
                        //cmsIcdCodes.Show(CurEditControl, e.Location);
                        CurEditControl.ContextMenuStrip = cmsIcdCodes;
                    }
               
                }

            }

           
        }

        private void dgDiagIns_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            this.CurEditControl = e.Control;
            cmsIcdCodes.Items.Clear();
            cmsIcdCodes.Items.Add("Add new here");
            CurEditControl.ContextMenuStrip = cmsIcdCodes;
               
        }

0
 
LVL 6

Accepted Solution

by:
RPCIT earned 300 total points
ID: 24367188
if you are wanting to fire the context meny on rightclick.. I think you will want to use the MouseDown event.. and test for e.button = mousebuttons.right..  or something like that.

I'm doing this from memory.. I don't remember the exact syntax to check the button.
0
 

Author Closing Comment

by:ArunVashist
ID: 31580224
Thanks RPCIT
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now