Selecting Grid view record and displaying record

chabian

New member
Joined
Nov 10, 2006
Messages
2
Programming Experience
5-10
What i want, is that when a user clicks on a row from a gridview that has about 5 columns with employee information (filled from a dataset), the rest of that employees information is filled into textboxes on the form below the grid. Can someone help with this or direct me to a good tutorial?

Thanks,

Chabes
 
You may try this out.

On cell mouse click or double click event, get the value of datagrid cell (depends on which value you may want to be as criteria on your sql query) of the current row then perform a sql query search to retrieve relevant data and display into your textbox.

You may take below code as reference.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DataGridView_CellMouseDoubleClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DataGridViewCellMouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] DataGridView.CellMouseDoubleClick[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dt [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable = getRecord(DataGridView.CurrentRow.Cells(0).Value[/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow[/SIZE]
[SIZE=2]dr = dt.Rows.Item(0)[/SIZE]
[SIZE=2]Textbox1.Text = dr([/SIZE][SIZE=2][COLOR=#a31515]"Field1"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]Textbox2.Text = dr([/SIZE][SIZE=2][COLOR=#a31515]"Field2"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]Textbox3.Text = Format(dr([/SIZE][SIZE=2][COLOR=#a31515]"Field3")[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
 
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] getRecord([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] employeeID [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] con [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FbConnection[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FbCommand[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FbDataAdapter[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dt [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]con.ConnectionString = conStr[/SIZE]
[SIZE=2]con.Open()[/SIZE]
[SIZE=2]cmd = con.CreateCommand[/SIZE]
[SIZE=2]cmd.CommandType = CommandType.Text[/SIZE]
[SIZE=2]cmd.Connection = con[/SIZE]
[SIZE=2]cmd.CommandText = [/SIZE][SIZE=2][COLOR=#a31515]"Select * From EMPLOYEE Where EMPLOYEE_ID = '"[/COLOR][/SIZE][SIZE=2] + employeeID + [/SIZE][SIZE=2][COLOR=#a31515]"'"[/COLOR][/SIZE]
[SIZE=2]da.SelectCommand = cmd[/SIZE]
[SIZE=2]da.Fill(dt)[/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] dt[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MsgBox(ex.Message)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
[SIZE=2]con.Close()[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
 
Noo.. you really wouldnt do that. Any of it. In this case youre using the datagridview like a nav control, so you point it to the same bindingsource as the rest of the data, limit the columns it shows and when you select a different row, the other bound controls will automatically update..

"Select * From EMPLOYEE Where EMPLOYEE_ID = '" + employeeID + "'"
In the interests of security, performance, reliability and dumping a really stupid, ancient programming foolishness, please never advocate forming SQLs like this. FOr more inforamtion as to why, read the PQ link in my signature..

also, given that you use .NET 2.0, I wouldnt recommend doing your data access like that at all. For more info, see the DW2 link in my signature :)
 
Take a look at the attachemnt I made. Start the app and look at the way form3 works.

The other forms show other concepts of data nav, including related data. I left them in for you to think about, but they arent related to your problem
 

Attachments

  • ComboAsDataNav.zip
    64.3 KB · Views: 33
Combo Box

Great, it works fantastic. I have another question I was hoping you might be able to help me with. I looked through the documentation but couldn't find an answer as it relates datasets and databound comboboxes.

Is there a way to fill the combo box items with data from another table, and have it below the data grid as in my first example, and still have it select the related employee record? Say in my employee table I have a 1 for Location. I have a Location table that has 1 as Los Angeles. When the user clicks on that employee record, Los Angeles is selected in the combo box, but all of the items from the Location table are in their so they can make the change if they want?

Thanks for any information you might be able to give me.
 
Great, it works fantastic. I have another question I was hoping you might be able to help me with. I looked through the documentation but couldn't find an answer as it relates datasets and databound comboboxes.

Is there a way to fill the combo box items with data from another table, and have it below the data grid as in my first example, and still have it select the related employee record? Say in my employee table I have a 1 for Location. I have a Location table that has 1 as Los Angeles. When the user clicks on that employee record, Los Angeles is selected in the combo box, but all of the items from the Location table are in their so they can make the change if they want?

Thanks for any information you might be able to give me.


Logically, You cant have the same combo box do both navigation and data editing. THink about it:
If the user chooses a new value from the combo, did they choose it because they wanted to nav there or did they want to edit the current record column to that value

Because this logic is simple, it is manifested simply:

If the combo is databound (meaning its .SelectedItem, .SelectedValue, .Text or other property is bound to a certain column of a datatable) then it always operates in edit mode.

If the combo is not databound (note that setting the .DataSource/.DisplayMember/.ValueMember is NOT databinding in the context here, it is merely telling the combo where to get its item list from) but it gets its values from a data based list (like a datatable) then selecting an item will nav the default view or currency manager of that list, and any other controls that are also using that currency manager


-

To achieve what you ask:

Have a grid, showing the contents of TblOrders
Have a combo for the city:
.DataSource = TblAllCities
.DisplayMember = "CityName"
.ValueMember = "CityID"
.SelectedValue is bound to TblOrders.ShippingCityID column

Changing the combo will edit the value of TblOrders.ShippingCity
 
Back
Top