?
Solved

how to get to underlying datarow from datagridview

Posted on 2012-12-21
14
Medium Priority
?
1,456 Views
Last Modified: 2012-12-21
I have a windows form that has a bound datagridview, which also has a checkbox column that is not bound.  I run through the gridview and get each row that is checked.  What I need to do then is to get to the underlying datatable that corresponds to this datagridview row.  The dataset table has been filtered before being bound to the datagrid.

thanks
0
Comment
Question by:mgmhicks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38713084
Is there a primary key column? If yes, get value of that cell and then you can get the underlying datarow using

Dim drow As DataRow() = dTable.Select("PrimaryColumnName=" & ValueFromGrid)
0
 

Author Comment

by:mgmhicks
ID: 38713331
For i = 0 To gvItems2.Rows.Count - 1
            If gvItems2.Rows(i).Cells(0).FormattedValue.ToString = "True" Then
                Dim myRow As DataRow = dsRoomItems.Tables(1).Select("ID=" & gvItems2.Rows(i).Cells(1).Value.ToString)

            Else

            End If

        Next

getting "Value of type '-1-dimensional array of system.data.datarow' cannot be converted to 'system.data.datarow'

any Ideas?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38713362
Select returns a Datarow array hence I had ()

 Dim myRow As DataRow() = dsRoomItems.Tables(1).Select("ID=" & gvItems2.Rows(i).Cells(1).Value.ToString)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mgmhicks
ID: 38713912
ok, almost there.  Here is the code I am running.  I am trying to move selected items in grid2 to items in grid1 then remove the items in grid2.  Not quite working leaving the 2nd item in the 2nd grid.  How can I change.  Thanks


        Dim i As Integer = 0
        Dim mystr() As String
        Dim myRow As DataRow()
        For i = 0 To gvItems2.Rows.Count - 1
            If gvItems2.Rows(i).Cells(0).FormattedValue.ToString = "True" Then

                myrow = dsRoomItems.Tables(1).Select("ID=" & gvItems2.Rows(i).Cells(1).Value.ToString)
                dsRoomItems.Tables(0).ImportRow(myRow(0))
                'dsRoomItems.Tables(1).Rows.Remove(myRow(0))
            Else
            End If
        Next

        Dim mRow As DataGridViewRow


        For Each mRow In gvItems2.Rows
            If mRow.Cells(0).FormattedValue.ToString = "True" Then
                myRow = dsRoomItems.Tables(1).Select("ID=" & mRow.Cells(1).Value.ToString)
                'dsRoomItems.Tables(0).ImportRow(myRow(0))
                dsRoomItems.Tables(1).Rows.Remove(myRow(0))
            Else
            End If
        Next
0
 

Author Comment

by:mgmhicks
ID: 38714064
i receive this error trying to remove the selected items from the grid view:
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

Here is the code:
 For i = 0 To gvItems2.Rows.Count - 1
            If gvItems2.Rows(i).Cells(0).FormattedValue.ToString = "True" Then
                myRow = dsRoomItems.Tables(1).DefaultView.Item(i).Row
                myRow.Delete()
                i = i - 1
            Else
            End If
        Next
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38714320
It can be any of the indexed items such as rows, cells, datatables that you are accessing using index. Do you have 2 DataTables inside the dsRoomItems dataset?
0
 

Author Comment

by:mgmhicks
ID: 38714450
yes, so I select from grid2 copy to grid1 then want to remove from grid2, dsroomitems has 2 tables 1 to grid1 and 2 to grid 2
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38714461
Try following

 For i = 0 to gvItems2.Rows.Count - 1
            If gvItems2.Rows(i).Cells(0).FormattedValue.ToString = "True" Then
                myRow = dsRoomItems.Tables(1).DefaultView.Item(i).Row
                myRow.Delete()
            Else
            End If
        Next


i is automatically decremented in a for loop.
0
 

Author Comment

by:mgmhicks
ID: 38714474
CodeCruiser that is what I was trying before I added the I = I-1 what I think is going on is that indexed or rows.count get messed up once I remove the row from the dataset.  Will removing it from grid instead take care of the underlying table?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38714475
Try following then


For i = gvItems2.Rows.Count - 1 To 0 Step -1
            If gvItems2.Rows(i).Cells(0).FormattedValue.ToString = "True" Then
                myRow = dsRoomItems.Tables(1).DefaultView.Item(i).Row
                myRow.Delete()
            Else
            End If
        Next
0
 

Author Comment

by:mgmhicks
ID: 38714545
this code sends both selected items over to grid1 however, grid 2 only get rid of 1 item, the 2nd in the checkedlist items, and leaves it checked.   Before the change both items would go over then leave the 2nd one in grid2.   Has got to be something with the index changing in datatable after removing an item.

But still getting same index error.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38714654
Index does change after deleting a row but that is why we are starting at the end and progressing to start so that index change does not affect us.
0
 

Author Comment

by:mgmhicks
ID: 38714673
looks like it is working now.  This seems a lot harder than what it is on a web form and datagrid, but I think we got it.  Not sure why it would be so hard, maybe it would be easier if I  bound that column.  Thank you very much. Your the best!!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38714688
Glad to help. I find winforms easier than webforms though :-)
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question