Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on 

Help with retreiving data from DataGridView

Hi,

If my DataGridView contains the following two columns, when I click on the first and fourth rows, how do I obtain the following two string variables

 s =  NSN, FIF?

ss = NSN Like DataGridView1.ROW(0).VALUE and FIF like DataGridView1.row(3).value?  

For the ss string I'm trying to retrieve he data entered in the Value column to build a search criteria for the fields listed in column(0).

Grid:

Fields               Value
NSN                  XXXXX
AGD                 YYYYYY
NASC               ZZZZZZ
FIF                   WWWW


Thanks,

Victor
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Victor Charles
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

You can refer to the Cells of the specific row, and get the .Value property of that Cell:

DataGridView1.Rows(0).Cells(0).Value

The above would get you the value in the first cell in the first row (the collection is zero-based), so if you need to get the 1st and 3rd, based on your grid:

DataGridView1.Rows(0).Cells(1).Value >>> this would return "XXXXX"
DataGridView1.Rows(3).Cells(1).Value >>> this would return "WWWW"

DataGridView1.Rows(0).Cells(0).Value >>> this would return "NSN"
DataGridView1.Rows(3).Cells(0).Value >>> this would return "FIF"
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Hi Victor,
Something like below.
1> To allow users to select the complete row set the selection mode of grid to row select in form load. (this is optional, only  information for you)
2> Then handle the cell click event to know that a cell was clicked and get the value of Fields column for that row.
3> In the cell click event, keep adding the string of Fields column of selected row to a List, before adding a check is present to ensure that it is not already added, if so, do not add.
4> Whenever string is required for filter, perform to String.Join(",",_selectedList), where _selectedList is the list containing all the string values added during Cell Click event. I have added a message box at the end of each cell click to show the Filter string formed till that time.
I have attached screenshots below of the output message box below.
Public Class Form1
    Private _dt As DataTable
    Private _selectedstrings As New List(Of String)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        _dt = New DataTable
        _dt.Columns.Add("Fields")
        _dt.Columns.Add("Value")
        _dt.Rows.Add("NSN", "XXXXX")
        _dt.Rows.Add("AGD", "YYYYY")
        _dt.Rows.Add("NASC", "ZZZZZ")
        _dt.Rows.Add("FIF", "WWWW")
        DataGridView1.DataSource = _dt
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub

  

    Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex < 0 Then
            Exit Sub
        End If
        Dim currentcellstring As String = DataGridView1.Rows(e.RowIndex).Cells("Fields").Value.ToString()
        If Not _selectedstrings.Contains(currentcellstring) Then
            _selectedstrings.Add(currentcellstring)
        End If
        MsgBox(String.Join(",", _selectedstrings))
    End Sub
End Class

Open in new window

User generated imageUser generated image
ASKER CERTIFIED SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Thank you for the solutions, I will try them and get back to you.
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Thank You.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo