none
how to get data from datagridview from another program RRS feed

  • Question

  • How to get data value from datagridview (contains text, combotext, checkbox, etc.) from another program which is not written by me and i have no code of it. So far I search and get function like findwindow, sendmessage, wm_gettext but i'm unable to retrieve data from datagridview with these function. For example, I want to detect if a checkbox checked or unchecked, which option of a combobox chosen. Can someone teach me how to do it?
    • Edited by qwn1990 Thursday, March 21, 2019 2:50 AM
    Thursday, March 21, 2019 2:28 AM

All replies

  • Hi,

    If you want to get value from datagridview from another program,you  better look for the data source of DataGridview.The following code is to monitor the control combbobox on the DataGridview.

    monitor code:

    Imports System.Runtime.InteropServices
    Imports System.Text
    Imports System.Data
    
    Public Class Form1
        <DllImport("User32.dll", EntryPoint:="FindWindow")>
        Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        <DllImport("User32.dll", EntryPoint:="FindWindowEx")>
        Public Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        <DllImport("User32.dll", EntryPoint:="FindEx")>
        Public Shared Function FindEx(ByVal hwnd As IntPtr, ByVal hwndChild As IntPtr, ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        <DllImport("user32.dll", EntryPoint:="SendMessageA")>
        Private Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As StringBuilder) As Integer
        End Function
        Public Shared WM_GETTEXT As Integer = &HD
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub getinfor()
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim maindHwnd As IntPtr = FindWindow(Nothing, "FormTest")
    
            If maindHwnd <> IntPtr.Zero Then
                MsgBox("Find Main Window")
                Dim maindHwndp As IntPtr = FindWindowEx(maindHwnd, 0, "WindowsForms10.Window.8.app.0.141b42a_r10_ad1", vbNullString)
                Dim com As IntPtr = FindWindowEx(maindHwndp, 0, "WindowsForms10.COMBOBOX.app.0.141b42a_r10_ad1", vbNullString)
    
                If maindHwndp <> IntPtr.Zero Then
                    MsgBox("Find Child Window!")
                    Const buffer_size As Integer = 1024
                    Dim buffer As StringBuilder = New StringBuilder(buffer_size)
    
                    SendMessage(com, WM_GETTEXT, buffer_size, buffer)
                    MsgBox(buffer.ToString())
                    ''SendMessage(maindHwndp, BM_CLICK, 0, Nothing)
                End If
            Else
                'MsgBox("No Find Main Window")
                'Environment.Exit(0)
            End If
        End Sub
    End Class
    

    another program code:

    Imports System.Data.SqlClient
    Public Class Form1
        Dim cmb_Temp As ComboBox = New ComboBox()
        Private Sub BindSex()
            Dim dtSex As DataTable = New DataTable()
            dtSex.Columns.Add("Value")
            dtSex.Columns.Add("Name")
            Dim drSex As DataRow
            drSex = dtSex.NewRow()
            drSex(0) = "1"
            drSex(1) = "male"
            dtSex.Rows.Add(drSex)
            drSex = dtSex.NewRow()
            drSex(0) = "0"
            drSex(1) = "famale"
            dtSex.Rows.Add(drSex)
            cmb_Temp.ValueMember = "Value"
            cmb_Temp.DisplayMember = "Name"
            cmb_Temp.DataSource = dtSex
            cmb_Temp.DropDownStyle = ComboBoxStyle.DropDownList
        End Sub
        Private Sub BindData()
            Dim dtData As DataTable = New DataTable()
            dtData.Columns.Add("ID")
            dtData.Columns.Add("Name")
            dtData.Columns.Add("Sex")
            Dim drData As DataRow
            drData = dtData.NewRow()
            drData(0) = 1
            drData(1) = "alex"
            drData(2) = "1"
            dtData.Rows.Add(drData)
            drData = dtData.NewRow()
            drData(0) = 2
            drData(1) = "zack"
            drData(2) = "1"
            dtData.Rows.Add(drData)
            drData = dtData.NewRow()
            drData(0) = 3
            drData(1) = "tom"
            drData(2) = "1"
            dtData.Rows.Add(drData)
            drData = dtData.NewRow()
            drData(0) = 4
            drData(1) = "peter"
            drData(2) = "0"
            dtData.Rows.Add(drData)
            drData = dtData.NewRow()
            drData(0) = 5
            drData(1) = "jam"
            drData(2) = "0"
            dtData.Rows.Add(drData)
            drData = dtData.NewRow()
            drData(0) = 6
            drData(1) = "jack"
            drData(2) = "1"
            dtData.Rows.Add(drData)
            Me.DataGridView1.DataSource = dtData
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Text = "FormTest"
            BindSex()
            BindData()
            Me.DataGridView1.Controls.Add(cmb_Temp)
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 21, 2019 6:17 AM
  • Like in a recent thread, you can access external controls by injecting a DLL (C++/CLI, easier to export the function) and execute a function to read the data with CreateRemoteThread


    • Edited by Castorix31 Thursday, March 21, 2019 8:36 AM
    Thursday, March 21, 2019 8:22 AM
  • Welcome Sir Alex Li-MSFT
    You have tried the code but it only gives one message which is the window
    Saturday, April 27, 2019 11:24 PM
  • Hello,

    Unless it's SQL-Server database and you are only interested in data CRUD operations you are looking at trial and error plus a decent amount of code which is also fragile as a change in the external app can break code you wrote to monitor for changes.

    Using code sure as Alex recommended you need to use a tool such as Microsoft Spy++ to integrate controls and sub controls of a app before even thinking of monitoring for changes. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, April 28, 2019 9:09 PM
    Moderator