Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 847
  • Last Modified:

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
0
Sanmarie
Asked:
Sanmarie
  • 9
  • 3
  • 2
  • +1
3 Solutions
 
iHadiCommented:
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;
0
 
BerdonMagnusCommented:
You may only have to invalidate the regions being colored, in your case the DataGridTextBoxColumn.
0
 
SanmarieAuthor Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DimkovCommented:
Yes. Create an event OnClick fro the dataklick, and call it from wherever you want to. This will simulate a click
0
 
SanmarieAuthor Commented:
Any code links on how to code this OnClick event
0
 
DimkovCommented:
Every control has it, and dategrid inherits from control.
so it should be

Namespace: System.Windows.Forms

protected virtual void OnClick (
      EventArgs e
)

ex.1
public class SingleClickTextBox: TextBox

{
      protected override void OnClick(EventArgs e)
      {
            this.SelectAll();
            base.OnClick(e);
      }


}

ex.2
private void button1_Click(object sender, System.EventArgs e)
{
   // Get the control the Button control is located in. In this case a GroupBox.
   Control control = button1.Parent;
   // Set the text and backcolor of the parent control.
   control.Text = "My Groupbox";
   control.BackColor = Color.Blue;
   // Get the form that the Button control is contained within.
   Form myForm = button1.FindForm();
   // Set the text and color of the form containing the Button.
   myForm.Text = "The Form of My Control";
   myForm.BackColor = Color.Red;
}
0
 
SanmarieAuthor Commented:
Ok. I will look at this code and get back to you.

Thanks

San
0
 
SanmarieAuthor Commented:
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

0
 
iHadiCommented:
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));
        }
0
 
SanmarieAuthor Commented:
Thanks iHadi,

Do you have the same code in VB.NET?
 I will try it and get back to you
0
 
iHadiCommented:
   Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Public Const WM_LBUTTONDOWN As Integer = 513
    Public Const WM_LBUTTONUP As Integer = 514

    Public Sub EmulateLMClick(ByVal x As Integer, ByVal y As Integer)
        SendMessage(Me.Handle, WM_LBUTTONDOWN, 1, ((y << 16) Or x))
        SendMessage(Me.Handle, WM_LBUTTONUP, 0, ((y << 16) Or x))
    End Sub
0
 
SanmarieAuthor Commented:
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
0
 
BerdonMagnusCommented:
How about resizing the column slightly then resizing it back (programmatically)? Should fire the paint event.
0
 
SanmarieAuthor Commented:
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
0
 
SanmarieAuthor Commented:
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.
0
 
SanmarieAuthor Commented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now