Link to home
Start Free TrialLog in
Avatar of Sanmarie
Sanmarie

asked on

How to simulate click event on datagrid

using .NEt 1.1, Windows forms, VB.NET

I have a datagrid where I color certain rows red. I had to override the DatagridTextBoxColumn class to get this to work. The user can filter the data on the datagrid. The problem is when the data is filtered, unless the grid is clicked, the paint event (that colors the rows red) does not get fired.

I have tried dataGrid.Focus and dataGrid.Refresh and it still doesn't work. I have to click on the grid for the coloring to take place. Is there a way to simulate the click event on the grid?

Thanks

San
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Hi

I'm using VS 2005 and I don't know if the following method is valid for VS 2003.
Try calling the Invalidate method of the control:

dataGrid.Invalidate();

This method forces the control to redraw itself;
Avatar of BerdonMagnus
BerdonMagnus

You may only have to invalidate the regions being colored, in your case the DataGridTextBoxColumn.
Avatar of Sanmarie

ASKER

Sorry for the delay in responding to you.

But your suggestions don't work. I still have to click the grid to see the colored regions. So is there a way to simulate a click event on a grid?

Thanks
San
Yes. Create an event OnClick fro the dataklick, and call it from wherever you want to. This will simulate a click
Any code links on how to code this OnClick event
SOLUTION
Avatar of Dimkov
Dimkov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok. I will look at this code and get back to you.

Thanks

San
Let me ask the question in a different way.

Is there a way to programmatically click the grid without physically using the mouse to do it. That is, I want the click event to occur from my program.

Help as I don't understand your solutions

San

The following code emulates a left mouse click at a specific point on a specific window.To use the code add the following line to the file's using block:

using System.Runtime.InteropServices;

Then you can use the following code:

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

        public const int WM_LBUTTONDOWN = 513;
        public const int WM_LBUTTONUP = 514;

        public void EmulateLMClick(int x, int y)
        {
            SendMessage(this.Handle, WM_LBUTTONDOWN, 1, ((y << 16) | x));
            SendMessage(this.Handle, WM_LBUTTONUP, 0, ((y << 16) | x));
        }
Thanks iHadi,

Do you have the same code in VB.NET?
 I will try it and get back to you
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks I added your code and it compiled fine but it doesn't solve my problem.

I still have to physically click on the grid for the row to be painted in red color. I debugged using the same x and y co-ordinates that I would click using the mouse and it doesn't work. Somehow the paint event of the DataGridTextBoxColumn only gets fired when I physically click on the grid or tab into it. Otherwise, it doesn't work.

Thanks

San
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

No, it still doesn't work. Even when I use the mouse to resize, the paint method doesn't get fired. Now, when I click, it works.

San
I just noticed something.

The rows do get painted red,  just not the first one.  I have to click into the grid for the first row to get painted. Strange, really strange.
I guess there is no solution. I will just keep tryhing alternatives. Thanks for your inputs though. If it's still a problem, I will pose a new question.

Thanks

San