DataGridView: Select Rows and retrieve the values of the cells  
Author Message
neogortex





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

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



Windows Forms10  
 
 
Peter Ritchie





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

This is off topic for the Visual Basic Language forum, moving to Windows Forms Data Controls and Databinding.

 
 
neogortex





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

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


 
 
Matty4242





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

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.


 
 
neogortex





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

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


 
 
neogortex





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

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.SelectedRows

              txtOut.Text = dgvRow.Cells("FrequencyUplink").Value.ToString

              Next

Thanks In Advance, Mike


 
 
neogortex





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

        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.SelectedRows

                       lstOut.Items.Add(dgvRow.Cells("FrequencyUplink").Value.ToString)

                 Next


 
 
Matty4242





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

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"


 
 
WS.abatec





PostPosted: Windows Forms Data Controls and Databinding, DataGridView: Select Rows and retrieve the values of the cells Top

Question 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