Copy Only Contents from DataGrdiView to DataTable

D

DIOS

I have unique setup which requires me to query several different
databases and then populate on DataGridView in my VB2005 app. All that
is working great but now I have the challenge of taking that DGV data
and converting it to a DataTable as there is an existing and rather
complicated subroutine that only takes a datatable as its argument.

Anyway an example of the dataset that I bind to the dgv initially

SELECT LONG_FIELD_XXXYY FROM SOURCE1;
-or-
SELECT JUST_AS_LONG_FIELDZZZZ FROM SOURCE2;

These are very simplified but I have way more fields than this and I
choose to change the dgv headers once the data is displayed like so

dgv.Columns("LONG_FIELD_XXXYY").HeaderText = "UserName"
-or-
dgv.Columns("JUST_AS_LONG_FIELDZZZZ ").HeaderText = "UserName"

The idea being the end user will see standard headers in the DGV
regardless of where the data came from. Again up to this point that is
working great. My goal now is to iterate either through the DGV or the
datatable that contains a copy of the data. But every time I do this
with

drow.Item("UserName")
-or-
drow.Cells("UserName").Value

I get an error that the field UserName does not exist. I think the
grid or table still use the underlying database field name and thus I
cant use the alias of the header. Is this true? Is there any way that
I can use my standard header of UserName to get cell values. I cant
figure out how to do this either with a DGV or a DataTable.

Any suggestions appreciated.

AGP
 
A

Aaron

This perhaps is not the most efficient way but it should work for you


Private Function GetCellValueByHeaderText(ByRef drRow As
DataGridViewRow, ByVal HeaderText As String) As String
Dim ReturnValue As String = String.Empty

For Each dc As DataGridViewColumn In drRow.DataGridView.Columns
If dc.HeaderText.Contains(HeaderText) Then
ReturnValue =
drRow.DataGridView.Rows(drRow.Index).Cells(dc.Index).Value.ToString()
End If
Next
Return ReturnValue

End Function
 
D

DIOS

This perhaps is not the most efficient way but it should work for you

    Private Function GetCellValueByHeaderText(ByRef drRow As
DataGridViewRow, ByVal HeaderText As String) As String
        Dim ReturnValue As String = String.Empty

        For Each dc As DataGridViewColumn In drRow.DataGridView.Columns
            If dc.HeaderText.Contains(HeaderText) Then
                ReturnValue =
drRow.DataGridView.Rows(drRow.Index).Cells(dc.Index).Value.ToString()
            End If
        Next
        Return ReturnValue

    End Function

thanks for the tip. i was thinking about doing something similar
except getting the index number of the column with the header and then
either using the index number or the underlying field property. seems
rather inefficient but that's all i have right now. any other
suggestions are appreciated.

AGO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top