Get the value of a cell on a ASPXGridview based on the button click - vb.net

I have a ASPXGridview with a button as the first column for each of the rows. When I click a button I want it to perform a callback which will get me the value of the cell next to the button click. I'm a bit stuck on this so any advise would be great.
Thanks

I created a call back function to get key field name from a row on selection changed.
Then when I click on the button is gets the selected keys on the page.

Related

Change the color of a selected record in an Access REPORT

My Access REPORT has a text box with the Record ID that looks like a button with an on click event to go to a form for that specific record. This works great, but when I return to the report I cannot see which record was clicked. I want to temporarily change ONLY the record that was clicked until another record is selected.
The reason I want this on a report and not a form is because I want the user to have a quick way to proof read in the format needed to print, and make a change or check a detail if necessary, then update the report AFTER all proof reading and updates are completed and before final print. But with many records on the screen it is easy to lose track of which record you were checking when returning from the form.
I tried:
Private Sub btn_txt_GoToTransaction_Click()
Dim vColor
vColor = RGB(51, 204, 51) 'green
Me.btn_txt_GoToTransaction.BackColor = vColor
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
But this does not work because every button turns color not just the selected record.
Any suggestions? Thanks.
This is a great question because there are many benefits to highlighting a row or item in an Access Report. You are not able to just change the button color in one row only, but you can highlight the whole row so the user knows where they were.
Here are two methods to accomplish this:
Method 1 - Click on a Label
This works great in newer versions of MS Access when using Report View. Use a Label Control instead of a Button. You could make the label look like a button if you format it that way. I prefer to stretch an invisible Label across the whole row on top of all the other controls in that row. Then if you click anywhere in the row, it automatically selects that row and then runs whatever code you have in the OnClick Event. This works best if the Label is not linked to a Text Box.
This picture shows an example of how this method looks. You can click anywhere in the row and it highlights that row with the red outline and grey background.
This is very simple and works well but there are a couple disadvantages:
1- You can not change the color of the highlight.
2- If any of the text boxes CanGrow, the row height may be higher then the Label and create areas where the invisible label doesn't capture your click.
3- Clicking on a Text box does not work for this method.
Method 2 - Change Color of a Text Box
In order to just highlight one row or one piece of data in a report, we can use the "FormatConditions" property. This is the same as Conditional Formating from the MS Access design interface but we are going to change it programmatically on the fly. You can't do this with a button or label - it needs to be a Text Box with unique data, such as your TransactionID.
This picture shows an example of how this method looks. You can set the color of the highlight if you follow the steps below.
STEP 1) I recommend that you add a text box to your report that stretches from the left to the right, set the Back Color and Fore Color to White, set the Control Source to TransactionID, and set the Name to TransactionID. Then right click on this text box and select Position > Send To Back. This works best if the other text boxes and labels on the report have a transparent background.
STEP 2) Add this code:
Private Sub HightlightRow(intRowID As Integer)
With Me.TransactionID.FormatConditions
.Delete
With .Add(acFieldValue, acEqual, intRowID)
.BackColor = vbGreen
.ForeColor = vbGreen
End With
End With
End Sub
STEP 3) Also change your button code to call this subroutine like this:
Private Sub btn_txt_GoToTransaction_Click()
HightlightRow Me.TransactionID.Value
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
STEP 4) I like to set it up so if the user clicks anywhere in the row, it will pop up with a modal with more detail regarding that row. Also, the user can't make any changes to the data in the Report View, so I use the pop up modal to allow changes. To accomplish this, I do a couple more things:
First, we need to add the code to the OnClick event for every control in that row. Ofcourse, each OnClick event will simply can that subroutine HightlightRow Me.TransactionID.Value
Second, if the user clicks on a Text Box, the Text Box gets the focus and hides the highlight. Therefore, I like to set the focus to something else. In your case, you could set the focus to the button by adding this line to the end of the HighlightRow subroutine: btn_txt_GoToTransaction.SetFocus
In my case, I am not using a button, so I set up a tiny Text Box with = " " (just an equal sign a space in quotation marks) as the Control Source. Then I position this tiny Text Box to the far right. And in the HighlightRow subroutine, I set the focus to this textbox.
STEP 5) You may also want a button or method of removing the highlight. To do that simply have the code run this line:
Me.TransactionID.FormatConditions.Delete

How can I set my ComboBox to allow the user to type in the first few characters and then automatically select the item by pressing ENTER?

I have a feeling this is a very simple thing that I'm overlooking.
I have two ComboBoxes that allow users to search for/select the record that they want to view. One is filled with Customer Names and the other is filled with Customer Numbers, so the user can look for a particular record by either selecting the Name or Number.
Each ComboBox is filled by a Data Table returned from a SQL Server database.
Each ComboBox has DropDownStyle set to DropDown, AutoCompleteMode set to SuggestAppend and AutoCompleteSource set to ListItems.
The user can either select by clicking the DropDown arrow and then clicking on the item they was or they can begin by typing and the ComboBox narrows the number of items in the list based on the characters the user is typing.
Using the mouse to click on the item in the list that they want works fine...it fires off a routine to retrieve the selected item from the database.
However, when the user types in the desired selection and presses ENTER, nothing happens. They must click the DropDown arrow and click on the item in order for the program to pull the appropriate record.
How do I get the ComboBox to pull the appropriate record when the user hits enter?
I'm using Visual Basic.
From the sounds of it, you need three events.
You need to use a timer to know when the user has stopped typing. To do that, you need one event would be when that field they're typing in has it's value change (<control's name>.TextChanged). That would start/restart a timer ticking (so that the user has a couple seconds to pause before the next event fires).
The next event would be the Tick event for that timer. That event would stop the timer, and then give focus to the right field so that when the user hits ENTER, they're not hitting ENTER in the field they've been typing in. You'll need to write a function to look up the right item in the ComboBox and call that.
Then you'd have a third event, either KeyPress, KeyDown, or KeyUp on the ComboBox itself. I'd lean towards the KeyUp to avoid issues if the user holds ENTER for whatever reason. That'd be what selects the item.
As a final FYI, I'm assuming you're using Visual Studio to write your code. If not, you should, and if you are/once you are, you can select the field you want to work with in the drop-down at the top left of the editor and then look at the associated events in the top right drop-down.
Thank you to JMichael for getting me on the right track with this one. I'm posting my solution here just in case it helps someone who has a similar question in the future:
The code that I added to the ComboBox's SelectionChangeCommitted event needed also to be added to the ComboBox's KeyUp event:
Private Sub cboPolicySearch_KeyUp(sended as Object, e As KeyEventArgs) Handles cboPolicySearch.KeyUp
If e.KeyCode = Keys.Enter Then
GetSelectedPolicySearchRecord()
e.Handled = True
End If
End Sub 'cboPolicySearch_KeyUp
The GetSelectedPolicySearchRecord() sub contained all of the information I needed to call my SQL Stored Procedure to select the data for the record that the user selected in the ComboBox.
Previously, this was only being called from the ComboBox's "SelectionChangeCommitted" event which is executed when the user clicks the drop down and then clicks a policy number from the drop down list.
I needed to add the same call to the GetSelectedPolicySearchRecord in the ComboBox's "KeyUp" event for when the user presses enter.

Selected Index and Cell Value in Detail SubForm

I'm trying to implement a very simple subform document grid on a form:
I have everything except getting the file path when the user clicks the grid. How can I get the filepath value from the user row click event ?
Sorry if my terminology is off I rarely write vba
You can create a public subroutine that references your FilePath field and then reference this subroutine in the on-click event of each of the fields on your subform.
So if your subform looks like this:
Go in to Design View of the form, and select one of your fields:
With a field selected, go to the Property Sheet on the right-hand side and go to the Event tab and click the [...] button on the On Click event:
You'll be taken to the form's VBA script module and it'll create the initial VBA for the on-click event of the field you selected:
Ignore that field's on-click event VBA for now, and instead move your cursor to the top and make some room for a public subroutine above the field's on-click event.
Write something like this at the top:
Public Sub GetFilePath()
Debug.Print Me.FilePath
End Sub
So your code in Access should now look something like this:
That public sub I've called GetFilePath can now be referenced in the on-click event of every field in your subform. Let's finish the on-click event of the ID field that we just started...
...and also reference the same public sub in every other field's on-click event (again, by selecting the field in Design View and then clicking [...] button in Property Sheet's on-click event):
In the VBA editor, make sure you have the Immediate Window open; it should be in the area below your VBA code. If it's not there press Ctrl + G or go to View > Immediate Window:
With the VBA editor and Immediate Window still open, go back to your form and put it into Form View.
Click on any row and you should see the FilePath data for the row you've clicked on print out in the Immediate Window (this is what Debug.Print does):
You probably don't want the FilePath to go the Immediate Window, but as you haven't specified where you want it to go I figured this would at least illustrate how you can get at that data by clicking on a record in your subform.
All you need to do is replace the Debug.Print Me.FilePath line to whatever is useful to you.
Hope this was enough to get you started though :)
MS Access does not provide Row click event. You have to either perform, [Form onClick Event] or ideally make the filePath as HyperLink and onClicking FilePath retrieve its value.
When you go for [Form onClick event] you will get the FilePath from selected row. But the click event is only fired if you are clicking the form and none of the fields.
As above mentioned, make the FilePAth field as hyperlink, add onClick event to it and retrieve the value.
sorry for upwarming but the accepted answer is not the best way to go.
Instead of creating an OnClick-event for every column in that subform (for each control),
you should rather use the OnCurrent event of the Subform itself, which is triggered whenever you change the current record. This basically happens whenever you click on another row in the subform.
Doing so will save you work whenever you extend the subform with a new column.
in that OnCurrent Event of the Subform you will then have the same code.
Basically it should look like this:
Public Sub Form_Current()
Debug.Print Me.FilePath
End Sub

Edit datagrid without clicking

I have a UltraGridWinGrid and I need to naviguate through it without using the mouse.
I can get to the grid with Tabs.
I can give focus to any rows with Tabs and Arrow Keys.
Once I highlight a row, how can I select a specific cell to edit it's content ? (All my cells are editable)
If you can modify the code that is executed when the TAB key is pressed, you can do manually select the next cell and start editing like this :
DataGridView.CurrentCell = theNextCell;
DataGridView.BeginEdit(true)
You still need to calculate which cell is next. This should be easy, increment the column index when you press tab, however if you reached the last column, just change row and set index to 0.
For the arrow keys, you could increment/decrement column or row index, depending of the arrow, and apply the same code as above.

Is it possible to Enter editmode to a list box in Grid without double click on the cell?

I was following the example in the following link to try out listbox in the grid
http://forums.silverlight.net/forums/t/53435.aspx
it works except that, I need to double click on the cell to enter to edit mode which then switch to listbox. Is there any other way I can enable list box on getting focus?
Thanks,
The is a very simple way to enter a cell in edit mode without using your mouse. First, you need to get the row in which you have the cell. Then you get the cell, then you change the edit mode to True. If you don't have the row, you can find it by using ItemContainerGenerator on the grid with the business object used in that row (from your ItemSource collection). Here is an example of code :
GridViewRow myRow = MyGrid.ItemContainerGenerator.ContainerFromItem(YourBusinessObject);
GridViewCell myCell = myRow.Cells(YourCellIndex);
myCell.IsInEditMode = True;
Hope this help !