Solved

Determine Text Selected in a DataGridView Cell

Posted on 2007-03-29
5
565 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

8 Experts available now in Live!

Get 1:1 Help Now