Solved

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

Posted on 2009-05-11
6
2,409 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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