Solved

Determine Text Selected in a DataGridView Cell

Posted on 2007-03-29
5
567 Views
Last Modified: 2013-11-26
I need to determine which text the user selected in a DataGridView cell.

For example, if the current cell value is "This is a Test" and the user selects the word Test, I need to get the word Test.
0
Comment
Question by:Kramarich
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 18820662
Can you please say a bit more about the context in which "if the current cell value is "This is a Test" and the user selects the word Test, I need to get the word Test"?

The reason for my question is this.  If the user right clicks on the cell when the word Test is selected then the context menu will allow the selected text to be copied to the Clipboard.  So, in that context, the facility is already available.  If we change the context, then there are all sorts of issues that arise.  For instance, the selectedtext is "selected" not in the cell itself but in the DataGridViewTextBoxEditingControl which replaces the cell itself during an edit operation.  And although it is available from that, whether that is available depends on when and how you want to get hold of the word.

Roger
0
 

Author Comment

by:Kramarich
ID: 18822904
I need to capture the word(s) the user has selected (highlighted) in the cell programatically.  I want the user to be able select a word or words in a cell and then press another button which will start a search for the selected word(s) and display the results.   So I need to capture what the user selected.

The DataGridViewTextBoxEditingControl looks like it has the property I want to use (DataGridViewTextBoxEditingControl.SelctedText) but I can't figure out how implement this.


0
 
LVL 34

Accepted Solution

by:
Sancler earned 250 total points
ID: 18824904
>>
The DataGridViewTextBoxEditingControl looks like it has the property I want to use (DataGridViewTextBoxEditingControl.SelctedText) but I can't figure out how implement this.
<<

Yes, that's the problem ;-).  Because it is an EditingControl it ceases to be accessible as soon as the user stops editing the cell.  Here's a demo of a workaround.  One form, one datagridview - called dgv - one button.  This code

Public Class frmGetSelText

    Private dt As New DataTable("DummyTable")
    Private myTextBox As System.Windows.Forms.DataGridViewTextBoxEditingControl
    Private mySelectedText As String

    Private Sub filltable()
        Dim dc0 As New DataColumn("ID", GetType(Integer))
        dt.Columns.Add(dc0)
        Dim dc1 As New DataColumn("Name", GetType(String))
        dt.Columns.Add(dc1)
        Dim dc2 As New DataColumn("Item", GetType(String))
        dt.Columns.Add(dc2)
        Dim dc3 As New DataColumn("Description", GetType(String))
        dt.Columns.Add(dc3)
        For i As Integer = 1 To 5
            Dim dr As DataRow = dt.NewRow
            dr(0) = i
            dr(1) = "Name" & i.ToString
            dr(2) = "Item" & i.ToString
            dr(3) = "Description" & i.ToString
            dt.Rows.Add(dr)
        Next
    End Sub

    Private Sub frmGetSelText_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        filltable()
        dgv.DataSource = dt
    End Sub

    Private Sub dgv_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellLeave
        If Not myTextBox Is Nothing Then
            mySelectedText = myTextBox.SelectedText
        End If
    End Sub

    Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
        If dgv.CurrentCell.ColumnIndex = 3 Then
            If TypeOf (e.Control) Is System.Windows.Forms.DataGridViewTextBoxEditingControl Then
                myTextBox = e.Control
            End If
        Else
            myTextBox = Nothing
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(mySelectedText)
    End Sub
End Class

It works with some dummy data, and is organised at the moment (although you could change that) so that only one column - the "Description" column, index 3 - provides the facility you want.  If you select some text in a record in that column, that will remain available in mySelectedText until something different is selected in a record in that column.  It is, as presented here, very rudimentary.  But it illustrates one way of getting hold of, and keeping, what otherwise ceases to be available once focus has moved off a cell that was being edited.

Roger
0
 

Author Comment

by:Kramarich
ID: 18828949
Thanks Roger!  With your sample code I was able to do what I needed.  However, I had to change a couple of things.  For some reason, when I went to use mySelectedText in the Button1_click event, the value in mySelectedText would be the value of cell (0,0) instead of the word I had selected from another cell.  I walked through the execution and the value of mySelectedText seemed to be getting set correctly but I just couldn't figure out how it was getting reset to the cell(0,0) value when I pressed Button1.

I got around the problem by doing the following:
  Eliminatied the dgv_CellLeave event.
  Eliminated the mySelectedText variable.
  In the Button1_Click event, used myTextBox.SelectedText instead of mySelectedText.

Thanks again for your help.  I'll accept your solution.
Ken
0
 
LVL 34

Expert Comment

by:Sancler
ID: 18828991
Ken

I'm not sure why that was happening.  I've just gone back to the test program and couldn't make it happen that way.  But It doesn't really surprise me.  So much depends on the details of the data and the binding set up and settings on other properties so far as where focus goes is concerned.  And it sounds as though, somewhere along the line, focus was going back - undirected and unnoticed and probably momentarily - to cell 0,0.  Still, you sorted it.  Good.

Roger
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

Suggested Solutions

Title # Comments Views Activity
Need help parsing JSON in my VB.Net application 4 45
how to read json value 2 31
C# Single Form 8 28
Point to Current Row Ater Refresh Datagridview 3 20
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

15 Experts available now in Live!

Get 1:1 Help Now