How to pass data from a DataGridView to another form?  
Author Message
reichard





PostPosted: Visual Basic Express Edition, How to pass data from a DataGridView to another form? Top

I have a DataGridView that contains a ButtonColumn. I want to be able to pass the data from the row the button is clicked to the form that is opened when the button is clicked. The names of the columns are Year, Set, Card Number, Number, and Condition. I have tried the following code, but I get a 'NullReferenceException was unhandled' for the line highlighted in red.

Public Class Catalog_Form
Dim TrackingList As New Tracking_List_Entry_Form
Dim SelectedRow As
Integer
Dim SelectedYear As
Integer
Dim SelectedSet As
String
Dim SelectedCardNumber As
String
Dim SelectedNumber As
Integer
Dim SelectedCondition As
String
Friend WithEvents Catalog_FormDataGridView As System.Windows.Forms.DataGridView
Friend WithEvents Tracking_List_ButtonColumn As _
System.Windows.Forms.DataGridViewButtonColumn

Private Sub Catalog_FormDataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CatalogDataGridView.CellClick

SelectedRow = Catalog_FormDataGridView.CurrentRow.Index
SelectedYear = Catalog_FormDataGridView.Rows(SelectedRow).Cells(0).Value
SelectedSet = Catalog_FormDataGridView.Rows(SelectedRow).Cells(1).Value
SelectedCardNumber = Catalog_FormDataGridView.Rows(SelectedRow).Cells(2).Value
SelectedNumber = Catalog_FormDataGridView.Rows(SelectedRow).Cells(3).Value
SelectedCondition = Catalog_FormDataGridView.Rows(SelectedRow).Cells(4).Value
TrackingList.Show()
Tracking_List_Entry_Form.Year.Text = SelectedYear
Tracking_List_Entry_Form.Make.Text = SelectedSet
Tracking_List_Entry_Form.Card_Number.Text = SelectedCardNumber
Tracking_List_Entry_Form.Number.Text = SelectedNumber
Tracking_List_Entry_Form.Condition.Text = SelectedCondition

End Sub
End
Class

Can anyone tell me how to get rid of the exception that occurs or what other code I can use to pass the data to the new form

Thanks,
John



Visual Studio Express Editions23  
 
 
spotty





PostPosted: Visual Basic Express Edition, How to pass data from a DataGridView to another form? Top

Firstly I'd check to see that a row was selected - as if no row was selected then you can you are looking at a null value.

If Catalog_FormDataGridView.SelectedRows.Count > 0 then

end if

Is good to check that at least one row has been selected

Then instead of using the rows with an index - why not simply use the selectedrows collection instead. Obviously if you have checked to see that there is at least one row selected then you can get the values out of the selectedrows collection - its just so much more simple than using rows and an index.

I think these two should sort your problem out.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedrows.aspx


 
 
reichard





PostPosted: Visual Basic Express Edition, How to pass data from a DataGridView to another form? Top

Spotty:

I now have the following code:

Private Sub Catalog_FormDataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CatalogDataGridView.CellClick
If Catalog_FormDataGridView.SelectedRows.Count > 0
Then
TrackingList.Show()
Tracking_List_Entry_Form.Year.Text = Catalog_FormDataGridView.SelectedRows(0).ToString
Tracking_List_Entry_Form.Make.Text = Catalog_FormDataGridView.SelectedRows(1).ToString
Tracking_List_Entry_Form.Card_Number.Text = Catalog_FormDataGridView.SelectedRows(2).ToString
Tracking_List_Entry_Form.Number.Text = Catalog_FormDataGridView.SelectedRows(3).ToString
Tracking_List_Entry_Form.Condition.Text = Catalog_FormDataGridView.SelectedRows(4).ToString
End
If
End Sub

I'm still getting the "NullReferenceException was unhandled" exception for the 'If' line when I click on the button from the DataGridView. What else do I need to do

John


 
 
reichard





PostPosted: Visual Basic Express Edition, How to pass data from a DataGridView to another form? Top

For those that are interested, after much trial and error, here is the code that I got to work:

Dim TrackingList As New Tracking_List_Entry_Form

Friend WithEvents Tracking_List_ButtonColumn As_ System.Windows.Forms.DataGridViewButtonColumn

Private Sub getCurrentCellButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CatalogDataGridView.Click

Dim msg As String = String.Format("Row: {0}, Column: {1}", _
CatalogDataGridView.CurrentCell.RowIndex, CatalogDataGridView.CurrentCell.ColumnIndex)

'ColumnIndex number 6 is the number of the column that contains the button column
If
CatalogDataGridView.CurrentCell.ColumnIndex = 6 Then
TrackingList.Show()
TrackingList.Player_InfoBindingSource.AddNew()
TrackingList.Year.Text = CatalogDataGridView.Rows_
(CatalogDataGridView.CurrentCell.RowIndex).Cells(0).Value
TrackingList.Make.Text = CatalogDataGridView.Rows_
(CatalogDataGridView.CurrentCell.RowIndex).Cells(1).Value
TrackingList.Card_Number.Text = CatalogDataGridView.Rows_
(CatalogDataGridView.CurrentCell.RowIndex).Cells(2).Value
TrackingList.Number.Text = CatalogDataGridView.Rows_
(CatalogDataGridView.CurrentCell.RowIndex).Cells(3).Value
TrackingList.Condition.Text = CatalogDataGridView.Rows_
(CatalogDataGridView.CurrentCell.RowIndex).Cells(4).Value
TrackingList.Cards_in_Set.Text =
"0"
Else
End If
End Sub


 
 
spotty





PostPosted: Visual Basic Express Edition, How to pass data from a DataGridView to another form? Top

From you answer I would assume that you are not using full row select on the datagridview - hence the selectedrows collection is not being populated.

If you are able to select individual cells then you would need to check the selectedcells collection and you can then get the rowindex and columnindex to get all the other records from this row.

The other approach will work IF you set SelectionMod to FullRowSelect at which point when you select a cell in a row the entire row would be selected.

Also you need to disable MultiSelect and set it to false. Otherwise its possible to select multiple cells which this code doesnt cater for. This is something you definately need to consider for this code your showing.

The suggested solution would simply require any column in a row to bve selected for the entire row to be selected. The Cells could use ordinal index value sbut would be better to use the Column names - making it more descriptive and allowing additional columns to be inserted without breaking the code.

Your solution depends upon the 7th cell to be the current cell. This will break or require modification if you insert a column in here - the value 6 is a magic number (it means nothing when just looking at the code) consider changing this to a constant which at least provides a named item which is a bit more descriptive.