Datagridview: how to get specific cell value in a row with reference to column name

rehan.azher

Member
Joined
Jan 1, 2009
Messages
11
Programming Experience
Beginner
Dear Experts,

I have a datagridview with following structure:


|Serial Number|Customer| Name| Level| Direction| Source Port| Sink Port| Working Route|


having multiple rows . now i want to access cell value under column Working Route of each row. can any one guide me for the same.

I am using following code to access each row.
VB.NET:
For Each gridRow As DataGridViewRow In DV2.Rows

Next
 
DataGridViews themselves do not store data. You would access the underlying datatable that the DGV is getting its data from:

Assuming your DGV is linked to a strongly typed dataset _myDataSet, and a table therein called MyDataTable:


VB.NET:
For Each ro as MyDataSet.MyDataRow in _myDataSet.MyDataTable

  MessageBox.Show(ro.WorkingRoute)

Next ro
 
VB.NET:
 Dim wBook As Workbook
        Dim eSheet As Worksheet
        Dim eapplication As Application
        Dim cPart As Range
        Dim lRow As Integer
        Dim lCol As Integer
        Dim loopRow As Integer
        Dim loopCol As Integer
        Dim mylist As New List(Of String)
        Dim str1 As String
        Dim str As String()

 '-------------------SET GRID HEADERS-----------------
                For loopCol = 1 To lCol
                    cPart = eSheet.Cells(1, loopCol)
                    DV2.Columns.Add(cPart.Value, cPart.Value)
                Next
                '----------------READ DATA TO GRID
                For loopRow = 2 To lRow
                    mylist.Clear()
                    For loopCol = 1 To lCol
                        If Not loopCol = lCol Then
                            cPart = eSheet.Cells(loopRow, loopCol)
                            mylist.Add(cPart.Value)
                        Else
                            cPart = eSheet.Cells(loopRow, loopCol)
                            str1 = cPart.Value
                            str1 = str1.Remove(InStr(cPart.Value, ";") - 1)
                            mylist.Add(str1)
                        End If
                    Next
                    str = mylist.ToArray
                    DV2.Rows.Add(str)
                    pb1.Value = pb1.Value + 1
                Next

Above is my code, i am populating Datagridview(DV2) from an excel sheet while opening excel as an application and accessing the required sheet.

my problem is that the excel sheet is being generated from another software , and it may have same columns with same headers but in different arrangements, so sometime i can not know which column is which and cannot hard code as i did above.
 
how to get specific cell value in a row with reference to column name

VB.NET:
icolumn = 0
            For Each grColumn As DataGridViewColumn In DV2.Columns
                If Trim(grColumn.HeaderText) = "Working Route" Then
                    icolumn = icolumn + 1
                    Exit For
                End If
                icolumn = icolumn + 1
            Next


thank you for the link dear , but i already did the job using above method , i know this is not a good practice but i think is better to change my whole coding from start to use above. thanks again.
 
thank you for the link dear , but i already did the job using above method , i know this is not a good practice but i think is better to change my whole coding from start to use above. thanks again.


Heh, I'd have used one of:
DV2.Columns.Item("Working Route").Index
DV2.Columns.Item("Working Route").DisplayIndex
 
Back
Top