reading cell data from a data grid

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
Hi can anyone help me with this. I have been following techgnome's excellent article Introduction to ADO.NET, Part 2 (http://www.developerkb.com/modules/wfsection/article.php?articleid=58) the article is great and has explained a lot to me about databases in VB.NET the problem i have is in the final part of the article having created the CurrentCellChanged event and copied the code into it i get an error saying it wont convert to a string. Heres the code from the Article
VB.NET:
[COLOR=#000080][B]        Dim[/B][/COLOR] CustID [COLOR=#000080][B]As[/B][/COLOR] [COLOR=#000080][B]String[/B][/COLOR] 
        CustID = Customers.Item(Customers.CurrentRowIndex, 0) 
 
        [COLOR=#000080][B]Dim[/B][/COLOR] DBConnection [COLOR=#000080][B]As[/B][/COLOR] New SqlClient.SqlConnection(MyCONNECTIONSTRING) 
        [COLOR=#000080][B]Dim[/B][/COLOR] SelectHistory [COLOR=#000080][B]As[/B][/COLOR] New SqlClient.SqlCommand("CustOrderHist", DBConnection) 
        SelectHistory.CommandType = CommandType.StoredProcedure 
 
        [COLOR=#000080][B]Dim[/B][/COLOR] SelectHistoryParameter [COLOR=#000080][B]As[/B][/COLOR] SqlClient.SqlParameter = SelectHistory.CreateParameter 
        SelectHistoryParameter.ParameterName = "@CustomerID" 
        SelectHistoryParameter.Value = CustID 
        SelectHistory.Parameters.Add(SelectHistoryParameter) 
 
        [COLOR=#000080][B]Dim[/B][/COLOR] HistoryAdaptor [COLOR=#000080][B]As[/B][/COLOR] New SqlClient.SqlDataAdapter(SelectHistory) 
        [COLOR=#000080][B]Dim[/B][/COLOR] ds [COLOR=#000080][B]As[/B][/COLOR] New System.Data.DataSet 
        Try 
            HistoryAdaptor.Fill(ds, "CustomerHistory") 
        Catch sqlex [COLOR=#000080][B]As[/B][/COLOR] SqlClient.SqlException 
            MessageBox.Show(sqlex.ToString) 
        Catch ex [COLOR=#000080][B]As[/B][/COLOR] Exception 
            MessageBox.Show(ex.ToString) 
        [COLOR=#000080][B]End[/B][/COLOR] Try 
 
        CustomerHistory.DataSource = ds 
        CustomerHistory.DataMember = "CustomerHistory
this the code in my test application where i have altered the code with no remedy.

VB.NET:
    Private Sub Customers_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Customers.CurrentCellChanged
        Dim CustID As String
        [B][I]CustID = Customers.Item(0, Customers.CurrentRow.Index)[/I][/B]
        
        Dim DBConnection As New SqlClient.SqlConnection(MyCONNECTIONSTRING)
        Dim SelectHistory As New SqlClient.SqlCommand("CustOrderHist", DBConnection)
        SelectHistory.CommandType = CommandType.StoredProcedure

        Dim SelectHistoryParameter As SqlClient.SqlParameter = SelectHistory.CreateParameter
        SelectHistoryParameter.ParameterName = "@CustomerID"
        SelectHistoryParameter.Value = CustID
        SelectHistory.Parameters.Add(SelectHistoryParameter)

        Dim HistoryAdaptor As New SqlClient.SqlDataAdapter(SelectHistory)
        Dim ds As New System.Data.DataSet
        Try
            HistoryAdaptor.Fill(ds, "CustomerHistory")
        Catch sqlex As SqlClient.SqlException
            MessageBox.Show(sqlex.ToString)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

        CustomerHistory.DataSource = ds
        CustomerHistory.DataMember = "CustomerHistory"
    End Sub
the exact error message i recieve in the error list is

Value of type 'System.Windows.Forms.DataGridViewCell' cannot be converted to 'String'.

can anyone tell me how this should be done in VB?
 
Ok i kind of got this to work i searched the forums here and dicovered that CellClick was the direction i should be going using VB.NET 2005 as i can now use the e parameter to determine which column/row was clicked. and i tried this
VB.NET:
CustID = Customer.item(0,e.rowIndex).ToString
this however returns a value 'DataGridViewTextBoxCell{ColumnIndex=0, RowIndex=n}' what i actually want is the contents of this Cell :confused:
 
i got it to work :) the solution was/is
VB.NET:
CustID = Customers.CurrentRow.Cells(0).Value
 
Last edited by a moderator:
Back
Top