Looping through datagridview

Posted by Ruchi22 under VB.NET on 10/15/2011 | Points: 10 | Views : 9675 | Status : [Member] | Replies : 8
Mine is a windows app. containing forms named BOM nd BOMSelected..

There is datagridview in BOM which contains checkbox column.. When the user selects checkbox, the selected rows should be seen in the datagridview of other form, SelectedBom..

I have coded but don't get it working.. Some error..

Can you please help ??

Your Help is greatly appreciated..

Hers is what i have done !!




Public Class SelectedBom

Private Sub SelectedBom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'HemDatabase1DataSet4.partno' table. You can move, or remove it, as needed.
'Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet4.partno)

Dim count As Integer = 0

For j As Integer = 0 To BOM.dgv1.RowCount - 1

If BOM.dgv1.Rows(j).Cells(0).Value = True Then

Dim ro As New DataGridViewRow
DataGridView2.Rows.Add(ro)

For i As Integer = 0 To BOM.dgv1.ColumnCount - 1
Me.DataGridView2.Rows(count).Cells(i).Value = BOM.dgv1.Rows(j).Cells(i).Value
Next

count += 1

End If

Next

End Sub
End Class



The output gives me "-1" in the first row, 1st coumn of second form.. Please Help !!




Responses

Posted by: Hmanjarawala on: 10/17/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi ruchi,
i think you can add datagridviewrow of one datagridview to another.
for that you have create new instance of 2nd datagridview and copy each cell of 1st datagridviewrow to the 2nd one.

Hope you got what i means.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hello.. Thank you so much for your suggestion.. I could understand what you are trying to say..

But since, i m using vb.net for the first time, i have no idea as to how and what should i write..

Can you please suggest me some code snippet for the same ??

Thank you again !!

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
Also, i now opted for an option to it.. I also tried using two datagridviews in one form for the same purpose..

1st is dgv1, which is bound to data source, and contains checkbox column..

When the user checks in the box, the selected rows should be displayed in the dgv3 on the same form, on click of the button..

I tried to debug and what I could conclude was that the rows with checkbox are read but there is a difficulty in fetching the data and displaying it in the other grid..

The code I use is :

[code]

Private Sub Btn_Transfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Transfer.Click
Dim IsRowSelected As Boolean
Dim SelectedValue As String
Dim i As Integer

'For Each row As DataGridViewRow In dgv1.Rows
'Dim chk As DataGridViewCheckBoxCell = TryCast(row.Cells(0), DataGridViewCheckBoxCell)

With Me.dgv3
'.Rows.Clear()
For i = 0 To Me.dgv1.RowCount - 1
IsRowSelected = Me.dgv1.Rows(i).Cells(0).Value
If IsRowSelected = True Then
.Rows.Add(1)
SelectedValue = Me.dgv1.Rows(i).Cells(0).Value
.Rows(.RowCount - 1).Cells(0).Value = SelectedValue
End If
Next

End With

'If Convert.ToBoolean(chk.Value) = True Then

'End If

'Next

End Sub

[/code]

Here, the problem seems to be somewhere in

[code]SelectedValue = Me.dgv1.Rows(i).Cells(0).Value[/code]

as this evaluates to the value "Nothing"..

I am also attaching the output I m obtaining..
 Download source file

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 10/18/2011 [Member] Bronze | Points: 25

Up
0
Down
Public Class SelectedBom

Private Sub SelectedBom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'HemDatabase1DataSet4.partno' table. You can move, or remove it, as needed.
'Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet4.partno)

Dim count As Integer = 0

For j As Integer = 0 To BOM.dgv1.RowCount - 1

If BOM.dgv1.Rows(j).Cells(0).Value = True Then

Dim ro As New DataGridViewRow

For i As Integer = 0 To BOM.dgv1.ColumnCount - 1
roCells(i).Value = BOM.dgv1.Rows(j).Cells(i).Value
Next

DataGridView2.Rows.Add(ro)

count += 1

End If

Next

End Sub
End Class


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
I tried to update the code.. But gives the same output.. -1 in first column, first row !!

The updated code is same as what you posted:

 Dim count As Integer = 0


For j As Integer = 0 To BOM.dgv1.RowCount - 1

If BOM.dgv1.Rows(j).Cells(0).Value = True Then

Dim ro As New DataGridViewRow

For i As Integer = 0 To BOM.dgv1.ColumnCount - 1
ro.Cells(i).Value = BOM.dgv1.Rows(j).Cells(i).Value
Next

DataGridView2.Rows.Add(ro)

count += 1

End If

Next


Thank you !! :)

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
I tried to debug, it is not entering the loop.. :(

Could you please help me figure out possible solutions for the same ??

Any help would be appreciated !!

Thank You..

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 10/18/2011 [Member] Bronze | Points: 25

Up
0
Down
will you send me your entire code? if you dnt mind

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ruchi22 on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
hey.. sorry for late reply.. i had tried attaching the file here but gave me an error.. May b server problems from my end.. :(

Neways, i opted other way and could solve it out !!

Thank you so ,much..

Ruchi22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response