Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Hi all,
1. My DataGridview is populated correctly and displays the data. No Problem here.
2. When then I use the Key Up/Down to navigate in the DataGridview the rows get highlighted when I pass over them. No problem here.
This is what I would like to code:
3. What I would like is that when the Rows gets highlighted when I pass over them that a Mouse click be executed on that row.
Is this even possible?
Regards,
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
What is it that you actually want? Obviously wanting a mouse click for it's own sake is pointless. You want some result that you think a mouse click will achieve. What is that result?
1 Attachment(s)
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
jmcilhinney
What is it that you actually want? Obviously wanting a mouse click for it's own sake is pointless. You want some result that you think a mouse click will achieve. What is that result?
Yes, there is a reason for this and I should have included it. Currently when I click on a row on the datagridview a calculation is performs that subtracts 2 dates and is displayed on lblDaysleft.Text. This is done in the Datagridview click event.
Details:
When the datagridview is loaded it populates the corresponding textboxes. One of those text boxes has a Warranty End Date (txtWRRTYEnd2.Text). I have the following code in the Datagridview Cell Click that calculates the remaining days of the warranty and displays it on the lblDaysleft.Text.
This code works as should when I click on the datagridview.
What I would like is that when a rows is highlighted when press Arrow key Down/Up to also perform the calculation.
Any suggestions is appreciated.
Please view attachment:
Private Sub dgvEdit_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEdit.CellClick
'Assigns the variable the date from a labeltextbox that loads
Dim todaysdate As DateTime = Me.lblTodayDate.Text
'declares variable and assigns the text value of textbox
Dim WrrtyEnddate As DateTime = Me.txtWRRTYEnd2.Text
Dim Daysleft as Int32 = WrrtyEnddate.Subtract(todaysdate).Days
Dim Results As String = Convert.ToString(Daysleft)
'displays the days left on the warranty
lblDaysleft.Text = Results
End Sub[/SUP]
Regards,
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Hard to say for sure because I don't know what this does WrrtyEnddate.Subtract(todaysdate).Day
But you can capture when a datagridview row changes in the "SelectionChanged" event. If the dgv uses a bindingsource you can use the bs PositionChange event.
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
The obvious solution is to handle a different event; one that is raised whether the user enters a row or cell with the mouse or the keyboard. The RowEnter and CellEnter events seem the obvious candidates.
That said, given that the contents of that event handler have nothing at all to do with the grid, I don't really see why you'd be using an event of the grid to execute that code.
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Dim WrrtyEnddate As DateTime = Me.txtWRRTYEnd2.Text
Dim Daysleft as Int32 = WrrtyEnddate.Subtract(todaysdate).Days
I didn't read this completely, I thought WrrtyEnddate was a class he had written. So, jmc is write. This code has nothing to do with your datagridview. So it's hard to understand what your trying to do.
1 Attachment(s)
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
wes4dbt
I didn't read this completely, I thought WrrtyEnddate was a class he had written. So, jmc is write. This code has nothing to do with your datagridview. So it's hard to understand what your trying to do.
• OK. Let’s put some perspective on this.
• The end result is that I get the number of days from two dates. That’s it!
• The code I showed you takes two date, converts them and shows the number of days between the two dates.
This is working just fine.
Let’s put this into context:
1. When I use the Up/Down Arrows on my keyboard and each row is highlighted it fills each textbox with their corresponding values.
2. The code executes when I click on the Highlighted row.
3. I placed a label on the form that has nothing to do with the Datagridview or Datasource that will display the results of the executed code.
What I ask in my posting is the following:
1. Can my code be executed when the row is highlighted?
2. I do not only want to use the DataGridView cell click event to execute my code, I also want it executed when the row is highlighted .
Also, would there be a better way of coding to get the number of days from tow dates?
Attachment 137677
Thank you.
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
1. When I use the Up/Down Arrows on my keyboard and each row is highlighted it fills each textbox with their corresponding values.
Yes, I named a couple of ways in post #4.
But why make the operator arrow thru the dgv to fill in the information?
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
wes4dbt
Yes, I named a couple of ways in post #4.
But why make the operator arrow thru the dgv to fill in the information?
The textboxes are bind to the same Data table as the DataGridView is bound to and populates the textboxes automatically as the DataGridView rows are highlighted using the arrows. I prefer to do it this way as a convenience instead of having the user click one row at a time to view the data in the textboxes. They can cruise through it quickly and select the one they are going to edit.
The code that I wrote works only if the user clicks on the row and then they will be able to see the results in the label, otherwise they will see a blank label.
That is why I am trying to see if its possible to simulate a mouse click when the user is using the Up/Down arrows.
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Usually you wouldn't trigger an exiting event handler from your code, you would call a sub.
So when the user clicks the mouse, in the click event you would call a sub to do the process you desired.
If you have another event occur where you would like to do that same process, you would call the same sub to do the process from that event.
For instance, if you moved the code from the mouse_click event into a sub and had the click event call it, you could also have the textbox change events all call that sub whenever their contents were modified, by user typing, or because the data the textboxes are bound to changed, etc...
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Based on your project description, I assume that you have a BindingSource instance in the code either explicitly created by yourself or by the designer.
Your desired result can be obtained by hooking into the BindingSource.CurrentItemChanged event.
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
TnTinMN
Based on your project description, I assume that you have a BindingSource instance in the code either explicitly created by yourself or by the designer.
Your desired result can be obtained by hooking into the
BindingSource.CurrentItemChanged event.
Could you provide a example on how to do this? I have looked for information on what you recommend but i cant find anything related.
I am not a developer and I have been doing this project on bits and pieces.
Regards,
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
lmalave
Could you provide a example on how to do this?
VB.Net Code:
Public Class Form1
Private WithEvents bs As New BindingSource
Protected Overrides Sub OnLoad(e As EventArgs)
bs.DataSource = New DemoTable
dgv.DataSource = bs
lblEndWarranty.DataBindings.Add("Text", bs, "EndOfWarranty", True, DataSourceUpdateMode.OnPropertyChanged, "", Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern)
UpdateDaysLeftInWarranty()
MyBase.OnLoad(e)
End Sub
Private Sub bs_CurrentItemChanged(sender As Object, e As EventArgs) Handles bs.CurrentItemChanged
UpdateDaysLeftInWarranty()
End Sub
Private Sub UpdateDaysLeftInWarranty()
Dim today As DateTime = DateTime.Today
lblToday.Text = today.ToString(Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern)
Dim eow As DateTime = CDate(DirectCast(bs.Current, DataRowView).Item("EndOfWarranty")).Date
lblDaysLeftInWarranty.Text = eow.Subtract(today).Days.ToString
End Sub
End Class
Friend Class DemoTable : Inherits DataTable
Public Sub New()
With Me.Columns
.Add("Field1", GetType(String))
.Add("EndOfWarranty", GetType(DateTime))
End With
Dim rnd As New Random
With Me.Rows
For i As Int32 = 1 To 5
.Add("Something_" & i.ToString(), Today.AddDays(rnd.Next(-5, 1000)))
Next
End With
End Sub
End Class
1 Attachment(s)
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
I will take a look at TnTinMN code later tonight.
This is what I was able to do but still has issues:
• I added another BUTTON (btnRunCodeClick) and added my code to it
• In the RowEnter of the datagridview I added the btnRunCodeClick.PerformClick().
• When I Move from one row to another it processes the code (the calculation) and display the results in the label.text as expected.
The problem now is that the calculation is for the previous row data and not the current selected data. It seems that it is always calculating for the previous row.
Any Suggestions.
Regards,
Attachment 137691
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
lmalave
In the RowEnter of the datagridview I added the btnRunCodeClick.PerformClick().
Or you could just put the code in a method and call it whenever you want it executed.
Quote:
Originally Posted by
lmalave
The problem now is that the calculation is for the previous row data and not the current selected data. It seems that it is always calculating for the previous row.
No, the problem is that you made assumptions about what should happen without reading the documentation. Here's what the documentation, accessible via the Help menu or the F1 key, has to say:
Quote:
This event occurs before the CurrentRow property is updated. To retrieve the index of the newly-entered row, use the DataGridViewCellEventArgs.RowIndex property within the event handler.
1 Attachment(s)
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
It’s now working as should!
First of all thank you everyone who provided their opinion and knowledge on this issue…
What I ended up doing was adding a label (lblRowCellDate ) that will get the date from the Datagridview selected row/cell directly. This date (variable) was then used to subtract the dates. This was done instead of using the date in the textbox that was bound to the Datagridview.
Working Code:
The code was placed inside a Button command.
The Button command is called from the Datagridview Rowenter event with (btnRunCodeClick.PerformClick())
'Assigns the variable todays from my form display date text [/COLOR]
Dim todaysdate As DateTime = Me.lblTodayDate.Text
‘Assigns the date from the column that holds the date needed to the label lblRowCellDate
lblRowCellDate.Text = dgvEdit.SelectedRows(0).Cells(27).Value
‘Assigns the variable WRRTYEnddate the datefrom the label lblRowCellDate
Dim WRRTYEnddate As DateTime = Me. lblRowCellDate.Text
‘ Assigns the variable DaysLeft the days that the two dates have (date from lblRowCellDate and todays date)
Dim DaysLeft As Int32 = departuredate.Subtract(todaysdate).Days
‘ Assigns the variable s2 the results of the calculation and conversion.
Dim s2 As String = Convert.ToString(DaysLeft)
‘ Assigns the label lblDaysleft the results
lblDaysleft.Text = s2
Regards,
Attachment 137721
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Yeah but as suggested you really should put the code in a sub. then it can be accessed for anywhere. You never know when you might want to make changes.
Code:
Private Sub CalcAndDisplay()
'Assigns the variable todays from my form display date text [/COLOR]
Dim todaysdate As DateTime = Me.lblTodayDate.Text
‘Assigns the date from the column that holds the date needed to the label lblRowCellDate
lblRowCellDate.Text = dgvEdit.SelectedRows(0).Cells(27).Value
‘Assigns the variable WRRTYEnddate the datefrom the label lblRowCellDate
Dim WRRTYEnddate As DateTime = Me. lblRowCellDate.Text
‘ Assigns the variable DaysLeft the days that the two dates have (date from lblRowCellDate and todays date)
Dim DaysLeft As Int32 = departuredate.Subtract(todaysdate).Days
‘ Assigns the variable s2 the results of the calculation and conversion.
Dim s2 As String = Convert.ToString(DaysLeft)
‘ Assigns the label lblDaysleft the results
lblDaysleft.Text = s2
End Sub
Button click event call CalcAndDisplay
dgv Rowenter event call CalcAndDisplay
Now you can use the code from anywhere in the class.
Re: Send Mouse Click to Row that is highlighted with Arrow Key Down/Up
Quote:
Originally Posted by
wes4dbt
Yeah but as suggested you really should put the code in a sub. then it can be accessed for anywhere. You never know when you might want to make changes.
Code:
Private Sub CalcAndDisplay()
'Assigns the variable todays from my form display date text [/COLOR]
Dim todaysdate As DateTime = Me.lblTodayDate.Text
‘Assigns the date from the column that holds the date needed to the label lblRowCellDate
lblRowCellDate.Text = dgvEdit.SelectedRows(0).Cells(27).Value
‘Assigns the variable WRRTYEnddate the datefrom the label lblRowCellDate
Dim WRRTYEnddate As DateTime = Me. lblRowCellDate.Text
‘ Assigns the variable DaysLeft the days that the two dates have (date from lblRowCellDate and todays date)
Dim DaysLeft As Int32 = departuredate.Subtract(todaysdate).Days
‘ Assigns the variable s2 the results of the calculation and conversion.
Dim s2 As String = Convert.ToString(DaysLeft)
‘ Assigns the label lblDaysleft the results
lblDaysleft.Text = s2
End Sub
Button click event call CalcAndDisplay
dgv Rowenter event call CalcAndDisplay
Now you can use the code from anywhere in the class.
Thank you for the suggestion and the code. I will definitely be adding the sub.
Thanks and regards,