Hello,

I wonder how it would be possible to get text from a datagridview in another form application? The datagridview has 5 rows and 5 columns with text in each cell.

I know that this could be achieved with windows API but googling around I have not understand how to actually do this.

My beginning of code finds the application and in somehow we would return all childcontrols that exists to find the datagridview and extract text from the cells?

        [DllImport("user32.dll")]
        private void button15_Click(object sender, EventArgs e)
        {
            foreach (Process clsProcess in Process.GetProcesses())
            {
                //We found the application
                if (clsProcess.MainWindowTitle.ToString().ToLower().Contains("nameofapplication"))
                {
                    //Now how will we find the datagridview and extract text from the cells?
                    Process[] processes = Process.GetProcessesByName(clsProcess.ProcessName);
                    foreach (Process p in processes)
                    {

                    }
                }
            }
        }

The better code attempt I have is the below. I have used the application WinSpy++ to identify the class name of the datagridview which is: "WindowsForms10.Window.8.app.0.141b42a_r13_ad1"
When running the code. The messageBox shows an empty string while the datagridview has alot of text in the cells.
I wonder if I am near a solution to get text from the datagridview?

static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
private void button15_Click(object sender, EventArgs e)
{
    foreach (Process clsProcess in Process.GetProcesses())
    {
        if (clsProcess.MainWindowTitle.ToString().ToLower().Contains("nameofapplication"))
        {
            Process[] processes = Process.GetProcessesByName(clsProcess.ProcessName);
            foreach (Process p in processes)
            {
                IntPtr pFoundWindow = p.MainWindowHandle;
                IntPtr childHandle;

                //Find window handle
                childHandle = FindWindowEx(
                    p.MainWindowHandle,    //Parent handle
                    IntPtr.Zero,    //Child window after which to seek
                    "WindowsForms10.Window.8.app.0.141b42a_r13_ad1", // Class name to seek (viewable in the Spy++ tool)
                    p.MainWindowTitle);


                const int WM_GETTEXT = 0x0D;
                StringBuilder sb = new StringBuilder();
                IntPtr retVal = SendMessage(childHandle, WM_GETTEXT, 100, sb);
                MessageBox.Show(sb.ToString());
            }
        }
    }
}

Edited 1 Year Ago by Darth Vader

This article has been dead for over six months. Start a new discussion instead.