Double Click a datagrid row

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

How can I just double click anywhere on a row and catch
the event? I just want to double click and get the the
value of a hidden ID column.

I have double click working, but only if you double click
the record selector, but I don't want a record selector
showing on the grid.

I would use a list view, but it doesn't support databiding
and I really don't want to get into wrapping it to make it
support binding.

It it just me or is the datagrid very flexible, but at the
same time a complete pain in the neck to use???

Cheers

Gary
 
Hi Gary,
Thanks for you post!
To my understanding now, you want to handle the double-click event on the
row of datagrid.
You need create a customized datagrid deriving from the Datagrid class,
override the OnMouseDown Event, to let the datagrid fire the doubleclick
event, you need skip the base.OnMouseDown handler, so you need handle the
row selection and mantain the current row index. Here is a code snippet.
<code>
class MyGrid : DataGrid
{
protected override void OnDoubleClick(EventArgs e)
{
MessageBox.Show("DoubleClick");
base.OnDoubleClick (e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
System.Windows.Forms.DataGrid.HitTestInfo ht = this.HitTest(new
Point(e.X,e.Y));
if (ht.Row >= 0)
{
if (ht.Type == DataGrid.HitTestType.Cell)
{
if (!IsSelected(ht.Row))
{
int count = BindingContext[this.DataSource,this.DataMember].Count;
for(int i = 0; i < count; i++)
UnSelect(i);
Select(ht.Row); // select the clicked row - do not call base
BindingContext[this.DataSource,this.DataMember].Position = ht.Row;
}
}
}
}
}
</code>
Does it solve your problem?
If you still have problem on this isue, please be free to reply to this
group.
Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Thanks Ying-Shen.

I must admit that the more I use .NET, the more frustrated
I become at the amount of times you have to derive your
own version of controls.

For me, one of the main drivers in moving to .NET was to
speed up application development. Now I'm finding I have
to spend more and more time implementing my own behaviours
for things that I would have expected to be fairly obvious
things to want to do.

I get the feeling that Microsoft are relying upon the
likes of Infragistics to provide controls and therefore
they provide little more than rudimentary controls.
At the very least I thought the ListView would support
data binding by now.

Anyway, rant over and thanks again for your help.

Gary

-----Original Message-----
Hi Gary,
Thanks for you post!
To my understanding now, you want to handle the double- click event on the
row of datagrid.
You need create a customized datagrid deriving from the Datagrid class,
override the OnMouseDown Event, to let the datagrid fire the doubleclick
event, you need skip the base.OnMouseDown handler, so you need handle the
row selection and mantain the current row index. Here is a code snippet.
<code>
class MyGrid : DataGrid
{
protected override void OnDoubleClick(EventArgs e)
{
MessageBox.Show("DoubleClick");
base.OnDoubleClick (e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
System.Windows.Forms.DataGrid.HitTestInfo ht = this.HitTest(new
Point(e.X,e.Y));
if (ht.Row >= 0)
{
if (ht.Type == DataGrid.HitTestType.Cell)
{
if (!IsSelected(ht.Row))
{
int count = BindingContext[this.DataSource,this.DataMember].Count;
for(int i = 0; i < count; i++)
UnSelect (i);
Select(ht.Row); //
select the clicked row - do not call base
BindingContext
[this.DataSource,this.DataMember].Position = ht.Row;
}
}
}
}
}
</code>
Does it solve your problem?
If you still have problem on this isue, please be free to reply to this
group.
Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!


.
 
Hi Gary,
Thanks for your feedback.

I fully understand you feeling, and we are always trying our best to make
winform easier and powerful. .NET programming is a new area and it may take
some time to master it. For this issue, we need to do some customization by
deriving a new control and changing its default behavior, which is almost
the same as we did before in VC 6.0 age.

For ListView databinding question, there is currently no way to do so. We
have already noticed it and have submitted feedback to our product group.
Please keep an eye for the future. We are looking at continual improvement,
and it's this kind of feedback that let us know what things you're trying
to do, that we haven't yet exposed for you.

Thanks again for participating the community. If there are any more
questions on Winform programming, please feel free to post here.

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Thanks again for participating the community. If there are any more
questions on Winform programming, please feel free to post here.

I have a related question .. I tried using the advice from above, but *my*
VS2003 installation has no class definitions "beneath" DataGrid .. ie the
class(es) System.Windows.Forms.DataGrid.HitTestInfo, HitTestType et al just
doesn't appear?

Nor can I find them through the class browser .. I can see the definition of
HitTest, the function of DataGrid, and it's supposed to return a
HitTestInfo, but clicking on it the class browser cannot find it, either.

Is this me, or is it my VS2003, or my install, or what? It's a MSDN
Universal one, installed when it was released. "Update" from the help menu
seems not to work, either, yet everything else net-wise do.

Sample I tried:

protected override void OnMouseDown(MouseEventArgs e) {
System.Windows.Forms.DataGrid.HitTestInfo ht = this.HitTest(new
Point(e.X,e.Y));

There is no "HitTestInfo".

/Micke - ??
 
Hi Micke,
I can't see the HitTestInfo in my VS2003 either, but the code can compile
through.
Did you get compile error?

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
Back
Top