Solved

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

Posted on 2009-05-11
6
2,462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

688 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