Question Move between GridView records and display them in Textbox

teymoorei

Active member
Joined
Jan 11, 2012
Messages
36
Programming Experience
1-3
Hello

I have a data grid, I want to move between the overlays by pressing the up and down arrow keys, and the data grid information will be displayed in the text box.

I did this now in the Cellclick event, but I want to do it in KeyUp and keyDown as well, but when I do this and move, it displays a previous record between the records.

This is the code I wrote in CellClick:

VB.NET:
txtWeight.Text = DataGridView2.CurrentRow.Cells(0).Value.ToString
cmbConsignmentType.Text = DataGridView2.CurrentRow.Cells(1).Value.ToString

And how can I go to the desired record with the arrow keys and select it by pressing the enter key?

Thank you for your guidance
 
If you were going to write code to do this, you should not be using either of those events. You should do it in one place that will cover all possibilities. What you actually care about is when the current row changes, ragardless of how it changes, so you should be looking for an even that is raised when that happens.

That said, you probably don't need any code at all. If your grid is bound then simply bind the same data source to the other controls too. If your grid isn't bound, change that so it is. Most commonly, a DataGridView will be bound to a DataTable that is populated from a database. You can bind the DataTable (or any other list) to a BindingSource and then bind that to both the grid and any other discrete controls, e.g.
VB.NET:
myDataAdapter.Fill(myDataTable)
myBindingSource.DataSource = myDataTable
myDataGridView.DataSource = myBindingSource
myTextBox.DataBindings.Add("Text", myBindingSource, "SomeColumn")
myComboBox.DataBindings.Add("SelectedValue", myBindingSource, "SomeOtherColumn")
Any selection you make in the grid will then be automatically reflected in the discrete controls and any changes you make in the discrete controls will be automatically reflected in the grid.
 
The VS.NET General forum is for general questions about the VS IDE, not for coding questions. Please post in the most appropriate forum for the topic, not the first one in the list. If your question is specific to Windows Forms then post in the Windows Forms forum. Thread moved.
 
You have complicated the matter

I've done this many times before, but I haven't programmed in a few years now, but I can't remember
I just want to scroll through the records with the arrow keys and hit enter
 
You have complicated the matter

No, it's really simple when you do it like jmc indicates. You can even code all this with a mouse, no keyboard. See the attached - it doesn't matter that it's in C#; it would be the same in VB because I wrote the entire thing with just the mouse, in about 90 seconds:

New .NET Framework project (data binding design is badly broken in .net core)
Add DataSet
Open DataSet
Right click DataSet surface
Add DataTable
Right click datatable
Add Column (repeat 2 more times)
Save
Open form designer
Show Data Sources tool panel (view menu.. other windows)
Drag DataTable1 node to form, grid appears
Expand DataTable1 node, drag children to form

Now have grid and 3 textboxes bound through the same bindingsource to the datatable. You didn't press a single keyboard key.

Run app, and put some data into the grid, make 2+ new rows with data, then click up and down them with the mouse. If you want to carry on with the no-keyboard mission, use right click copy/paste to get some data from somewhere like the web and paste it in :D
 

Attachments

  • WindowsFormsApp1.zip
    18.6 KB · Views: 7
Last edited:
Back
Top