remove first 2 characters from column in datatable

Phil_Pearce

Member
Joined
Dec 30, 2009
Messages
6
Programming Experience
Beginner
I would like to remove the first 2 columns in the datatable if the first character is not numeric. Here is my code so far.

VB.NET:
For Each DRow As DataRow In aTable.Rows
                    Dim cRow As String
                    cRow = DRow.Item("Column 4").ToString()

                    If IsNumeric(Left(cRow, 1)) Then

                    Else
                        ??????
                    End If
                    
                Next
How do I remove the first 2 characters?
 
Use right and len

Just replace the ????? with:

cRow=right(cRow, len(cRow)-2)​

That just changes the cRow string. You will then need to write that new data back to your data table, assuming that is what you want/need to do with it.

Note that you may need to prefix 'Left' and 'Right' in your code with 'Microsoft.VisualBasic.'
 
To update the datatable would it just be the case of putting the below code after striping of the characters in the IF statement?
VB.NET:
DRow.AcceptChanges()
 
When I first saw your question, I wondered if you had already worked out how to deal with extracting information from the database and then reinserting it. I assumed yes.

However, I'm not sure that you have. I have done very little database programming so probably won't be of much help except to say, have you tried your code? Does cRow = DRow.Item("Column 4").ToString() get you what you need? If so then I would have thought that reversing the process is what is needed. Something like

DRow.Item("Column 4") = cRow

might do it but I do not know what format your data table uses so do not know if passing a string back into the table will work. After making the changes, then I expect the accept changes command would be required to make the changes stick.

Sorry but you really need to know how your database works before trying to manipulate the data in it. Maybe someone else with knowledge of database programming can help better than I.
 
Back
Top