Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Navigating With DataGridView - What Is The Current Record?
 

Navigating With DataGridView - What Is The Current Record?

Can anybody help a VFP Developer trying to understand how a VB.NET DataGridView (VS2005) works?

What I want to do is to have a grid on a form with a list of records, and a button which (for now) displays the PK of the currently highlighted record.

In my form Load() I have (cusBO.GetAllRecords() is a Business Object method which returns a DataTable - this is returning the correct data):

Dim dt1 As New DataTable

dt1 = cusBO.GetAllRecords()

Me.BindingNavigator1.BindingSource = Me.BindingSource1

Me.BindingSource1.DataSource = dt1

Me.DataGridView1.DataSource = Me.BindingSource1

The form runs, and I can navigate either on the grid or with the BindingNavigator.

Now I need some code to call in the button's Click() to essentially give me:

MsgBox(<current record PK field value>)

This is the bit I need some help with please.

TIA, Kevin

kdwms  Tuesday, October 16, 2007 12:58 PM

Hi, Kevin

Base on my understanding ,you want to show the current cell's value. You can try something like this

Code Block
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'MessageBox.Show(Me.DataGridView1.CurrentRow.Index.ToString)
MessageBox.Show(Me.DataGridView1.CurrentCell.Value.ToString)
End Sub

If you have any further questions, feel free to let me know.

Best Regards

Gavin

Gavin Jin - MSFT  Wednesday, October 17, 2007 5:00 AM

Hi, Kevin

Base on my understanding ,you want to show the current cell's value. You can try something like this

Code Block
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'MessageBox.Show(Me.DataGridView1.CurrentRow.Index.ToString)
MessageBox.Show(Me.DataGridView1.CurrentCell.Value.ToString)
End Sub

If you have any further questions, feel free to let me know.

Best Regards

Gavin

Gavin Jin - MSFT  Wednesday, October 17, 2007 5:00 AM

Hi Gavin,

Thanks very much, that was very helpful. However, I need to make it a little more complicated. What I need to return is the primary key field value from the current row, which is in the source datatable, but not set as one of the columns in the DataGridView?

I also afterwards want to move on from there to, instead of clicking on a button, being able to automatically fire the messagebox (for now, I intend hitting another B/O method and then populating other controls beside the grid later) whenever the user navigates to a different row, either on the grid or the BindingNavigator.

Regards,

Kevin

kdwms  Wednesday, October 17, 2007 6:24 PM

You can use google to search for other answers

Custom Search

More Threads

• Adding new record with gridview
• Multi-page printing of a DataGridView table
• Datagrid select
• DataGridView STILL adds two new rows on 'escape'
• VB to .net conversion, error in execution
• please help me about update and delete
• No append Row
• What can I Use instead Datamodule in C#
• DatagridviewComboboxColumn Draw Text Only
• testing a where clause