DataGridView: Select Rows and retrieve the values of the cells
-
Tuesday, August 29, 2006 2:58 PM
Language: VB.NET 2005
Framework: 2.0
Database: SQL Server Express 2005
I have a DataGridView that is populated with data from a SQL Server DB. I need to allow the user to select Rows of interest and then the values of some of the cells on the selected Rows needs to be retrieved for Math calculations.
I can retrieve values from cells I just need to know how to get VB to notice that a Row has been selected and only retrieve the vallues on the selected Rows.
How to retrieve Values from a DataGridView - May Help Someone
OutPut = DataGridViewName.Item(columnIndex As Integer, rowIndex As Integer).Value.ToString
txtOut.Text = grdCboTest.Item(3,0).Value.ToString
Thanks in advance for any help, Mike
All Replies
-
Tuesday, August 29, 2006 3:04 PMThis is off topic for the Visual Basic Language forum, moving to Windows Forms Data Controls and Databinding.
-
Tuesday, August 29, 2006 3:36 PM
The Code Below Will Only Retreive One Value Out Of All The Selected Rows - The Next Answer Down Will Retreive All The Values From The Selected Rows
I found the answer in the post below, incase anybody else would like to know the answer
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=276684&SiteID=1
'I made the change to the code as shown below and I was able to retreive the value of a cell in a selected Row
'loop through every item in DataGridView
For Each SelectedRow As DataGridViewRow In _grdCboTest.SelectedRows
txtOut.Text = SelectedRow.Cells(
"FrequencyUplink").Value.ToString Next -
Tuesday, August 29, 2006 3:42 PM
You can use the DataGridViewName.SelectedRows property to get all the currently selected rows as a collection. Use this in conjunction with the DataGridViewName.MultiSelect property to allow the user to select a single or multiple rows at a time.
You can then access each selected row as follows (pseudocode):
Dim dgvRow as DataGridViewRow
For Each dgvRow in DataGridViewName.SelectedRows
Debug.print (dgvRow.Cells(0).Value) ' Shows value form first column of each selected row
...
Next
Hope this helps.
-
Tuesday, August 29, 2006 3:46 PM
Cool, thank you very much, I was just working on being able to select multiple values. The code I pulled off the forum only pulls one value.
Thank You very Much, Mike
-
Tuesday, August 29, 2006 4:07 PM
This still shows only one value - Am I entering the values wrong?
FrequencyUplink - is one of the Columns that I'll need to retreive all of the values from when the Row(s) is selected
Dim dgvRow as DataGridViewRow
'loop through every item in DataGridView For Each dgvRow In grdCboTest.SelectedRowstxtOut.Text = dgvRow.Cells(
"FrequencyUplink").Value.ToString NextThanks In Advance, Mike
-
Tuesday, August 29, 2006 5:23 PM
Ok, my fault, I was directing the output to a texbox with multiline = True and I'm not sure why but the Loop would not add the values to multilines in a textbox only one value would show. I added a list box and now I can see all of the values of all the selected Rows.
grdCboTest = Name of DataGridView
lstOut = Name of List Box
FrequencyUplink = Column Name in the DataGridView
Dim
dgvRow as DataGridViewRow 'loop through every item in DataGridView For Each dgvRow In grdCboTest.SelectedRowslstOut.Items.Add(dgvRow.Cells(
"FrequencyUplink").Value.ToString) Next -
Sunday, September 03, 2006 2:39 PM
Mike:
To add multiple lines to a multiline text box, you need to separate each line by a vbCrLf as in
txtOut.Text - "Line 1" & vbCrLf & "Line 2"
Or to succesively add lines, use
txtOut.AppendText "new line to be added at end"
-
Thursday, November 29, 2007 10:51 AMQuestion to FOR each SELROW
I have a Datagridview with records ordert ASC
after
For Each SelectedRow As DataGridViewRow In DataGridView1.SelectedRows
dim value as string = (SelectedRow.Cells("FileldName").Value.ToString)
Next
I get all values in descending order.
Is there a way to order the direction ??
Thank for help
Wolfgang -
Wednesday, September 28, 2011 2:21 AM
This is off topic for the Visual Basic Language forum, moving to Windows Forms Data Controls and Databinding.
good answer


