Access and control Datagridview of external winform app?

bhavtosh

New member
Joined
Jun 26, 2019
Messages
2
Programming Experience
1-3
Hello,
I have a .net winform app with 1 form and 1 dgv control.
I searched and found some ways like windows api and ui automation stuff but i could not find more details to perform above actions especially with datagridview control example.

562


Can someone kindly suggest or share some starting points so that i can move ahead? Im badly stuck with this requirement for days....
What i need is a way to access that datagridview and manipulate the data from another winform app in few ways like:
1. copy/paste single or multiple rows
2. edit data/rows
3. right click on a row
I searched and found some ways like windows api and ui automation stuff but i could not find more details or any example to perform above actions especially with datagridview control.

Can someone kindly suggest or share some starting points so that i can move ahead? Im badly stuck with this requirement for days....
Thanks....
 
There are some approaches:
Approach 1: Use raw Win32 APIs
Essentially page scrape the DataGridView using multiple calls to EnumWindows() and GetWindowText(), and then use the Win32 input APIs to change focus to the appropriate cells and enter new text. Yuck!

Approach 2: DLL injection
You can try to write some code into a .NET assembly which does all the DataGridView manipulations. Then you need write some assembly, C, or C++ code to do some DLL injection to inject some code into the process space of that other program. The objective is for the injected code to then get into the managed space of the other process and effectively call LoadAssembly() to load your .NET assembly, and then once loaded to execute it. Yuck!

Approach 3: Use Windows UI automation
Let the Windows UI automation do all the hard work of finding the other process' screen elements and do the inputs that you would have had to write in Approach 1. I recommend going down this path. This is not impossible. This is how automated testing happens. This is the basis for a lot of the assistive technologies which help people with disabilities be able to use Windows programs.

Approach 4: Use AutoHotkey
You could say, the learning curve for the Windows UI automation APIs is too steep, and just learn how to script things using AutoHotkey.

Personally, though, I would try to get in touch with the original author of the other program to see if they have extension points. Or if the other program is open source and whose license lets you modify the code, then it would be better to change the original code to support what you need to do.
 
There are some approaches:
Approach 1: Use raw Win32 APIs
Essentially page scrape the DataGridView using multiple calls to EnumWindows() and GetWindowText(), and then use the Win32 input APIs to change focus to the appropriate cells and enter new text. Yuck!

Approach 2: DLL injection
You can try to write some code into a .NET assembly which does all the DataGridView manipulations. Then you need write some assembly, C, or C++ code to do some DLL injection to inject some code into the process space of that other program. The objective is for the injected code to then get into the managed space of the other process and effectively call LoadAssembly() to load your .NET assembly, and then once loaded to execute it. Yuck!

Approach 3: Use Windows UI automation
Let the Windows UI automation do all the hard work of finding the other process' screen elements and do the inputs that you would have had to write in Approach 1. I recommend going down this path. This is not impossible. This is how automated testing happens. This is the basis for a lot of the assistive technologies which help people with disabilities be able to use Windows programs.

Approach 4: Use AutoHotkey
You could say, the learning curve for the Windows UI automation APIs is too steep, and just learn how to script things using AutoHotkey.

Personally, though, I would try to get in touch with the original author of the other program to see if they have extension points. Or if the other program is open source and whose license lets you modify the code, then it would be better to change the original code to support what you need to do.

Thanks for reply...
Approach 1, i tried that already, it is easy as dgv control has no classname like other window controls, so using enum child windows & other functions wont work
Approach 2, Im not very techie to try this :)
Approach 3, I need some examples or articles to dig more as msdn info is limited
Approach 4, will surely explore

I didnt know dgv control will be so tough to crack unlike textbox, button, treeview etc controls...
 
I didnt know dgv control will be so tough to crack unlike textbox, button, treeview etc controls...
That is because most of the other WinForms controls are simple wrappers around the other Win32 API controls. The DataGridView was written from the ground up as a WinForms control to supplement all the deficiencies people have been complaining about regarding the Visual Basic DataGrid.ocx control.
 
Approach 3, I need some examples or articles to dig more as msdn info is limited
I disagree, MSDN information is very extensive. You just need to start reading. Of particular interest to you would be:

It feels to me that what you actually is just some ready to run code that addresses how to access the data grid view, rather than learning how things work and be able to do things yourself.
 
Back
Top Bottom