Solved

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

Posted on 2009-05-11
6
2,301 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ArunVashist
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks RPCIT
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

9 Experts available now in Live!

Get 1:1 Help Now