Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing CurrentCell on Leave event of datagrid

Status
Not open for further replies.

ookete

Programmer
Oct 5, 2004
180
0
0
US
Hello, I want to capture the CurrentCell of a datagrid when the user gives focus elsewhere (using the datagrid's Leave event). This works just fine if the cursor was in a populated row, but if the cursor is in a "new" row that does not yet have data, then the CurrentCell property gives me the last row number of the populated set... not the new row that the cursor was in. I imagine this is because the row does not really exist yet because it has no data, but how can I capture that condition?

Any advice is appreciated.
 
I'm not for sure if this is a "good" way of doing it, or a "Ghetto" was of doing it. But anyhow it captures what your looking for.

Basically what can do is monitor the current cell in the "curentCellChanged" event. Inside the method you could create a variable, and then use it to find out what the last cell was clicked on.

Private Sub Datagrid2_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Datagrid2.CurrentCellChanged
Dim lastCell As String
lastCell= Datagrid2.CurrentCell.ToString
End Sub

Anyhow with a little pulling and tugging you can get the information you need out of that variable.

-Ghetto programmer mike
 
oh ya, that was ghetto, make sure declare the variable outside of the "CurrentCellChanged" event if you want to use it.

-mike
 
can you try ActiveRow and ActiveCell ?Or you can use the rows.count and get the unpopulated row or else try rowcolchange event?
 
Hmmm, I tried this and had an interesting result. I made two labels on the form outside of the datagrid, one for row number and one for column number. The CurrentCellChanged event updated these numbers as you would expect, even with "new" rows that had no data. However, if I was on a new row and I left the datagrid (focus change), the CurrentCellChanged event fired and the rownumber of the CurrentCell reverted to the highest existing row number (instead of remaining at the "new" row number). It's as if a datagrid shifts the CurrentCell up a row if you leave the grid before the new row is established with data.

This is a problem for me because I have a custom number keypad that I created to insert data into the grid. This keypad needs to know what cell to place its value into, but it can't do so for new rows because the grid keeps negating my CurrentCell value.

Not sure what to do or how to ignore that last CurrentCell shift...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top