Hi!

This is actually not a question, but I want to share what I realized when I was doing some projects using DataGridView. I was a bit confused on what I should use for the cells whenever I check the value of a DataGridCell (e.g. String.IsNullOrEmpty() ).

Before I usually do it like this:

If Not String.IsNullOrEmpty(dgv.Rows(0).Cells(0).Value.toString()) Then
Dim str As String = dgv.Rows(0).Cells(0).Value.toString()
End If

But I sometimes get an error when the value of the cell is nothing. So I realized that instead of using toString(), I should have been using CStr().

If Not String.IsNullOrEmpty(CStr(dgv.Rows(0).Cells(0).Value)) Then ...

But for datatable, it is the opposite. CStr(dt.Rows(0).Items(0)) would generate an error if its value is DBNull. So instead of using CStr(), toString() is the correct method to use : dt.Rows(0).Items(0).toString() I was really confused before as to which to use in DataTable and in DataGridView. So I hope this will help someone in using DataTable and DataGridView.

Thanks!

What I have noticed is:
CStr() method will convert the value IN the object to a string.
ToString() method will convert the value OF the object to a string.

ToString is available for a wide range of objects.
CStr is a type conversion function which converts other data type to string data type.

Is anyone now following the loop in this area??????

I wanted to know the exact difference between both CStr and tostring


Can anyone please help me in this ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.