Results 1 to 6 of 6

Thread: [RESOLVED] How to put certain textstrings to certain columns same row?

  1. #1

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    Resolved [RESOLVED] How to put certain textstrings to certain columns same row?

    Hello people,

    I have made a sql query that stores the query result into a data-table.
    my issue is now...

    I need to put my text-strings (str1,str2,str3 and str4)
    to certain columns and same row (in the same loop)

    below is my code.
    Code:
            For Each dt2row As DataRow In HashDataTable4.Rows
    
                Dim CmString As String = (dt2row.Item(0).ToString()) ' Here is how I get my textstring.
    
                Dim str1 As String = dt2row.Item(0).ToString()
                str1 = Regex.Replace(str1, "(\d{5})", "$1") ' ordernr
    
                Dim str2 As String = dt2row.Item(0).ToString()
                str2 = Regex.Replace(str2, "(\d{5})(.*)(Language: )(\w{3})(.*)", "$4") ' source language
    
                Dim str3 As String = dt2row.Item(0).ToString()
                str3 = Regex.Replace(str3, "(.*)(\w{3})(-)(\w{3})(.*)", "$4") ' target language
    
                Dim str4 As String = dt2row.Item(0).ToString()
                str4 = Regex.Replace(str4, "(.*)(Context Match: )(.*)(;)", "$3") ' Context Match
    
            Next
    Could someone help me?

    thank you in advance

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How to put certain textstrings to certain columns same row?

    One thing that I notice is that you're getting the same object 5 times:
    Code:
    dt2row.Item(0).ToString()
    Instead, you should be storing that value in a variable(which you're doing) and then getting your substrings from that variable(which you're not doing).

    As far as setting the cell value of the same DataRow that you're currently iterating then it's as easy as getting the value only instead of just getting the value you'd be setting it:
    Code:
            For Each dt2row As DataRow In HashDataTable4.Rows
    
                Dim CmString As String = (dt2row.Item(0).ToString()) ' Here is how I get my textstring.
    
                dt2row.Item(1) = Regex.Replace(CmString, "(\d{5})", "$1") ' ordernr
                dt2row.Item(2) = Regex.Replace(CmString, "(\d{5})(.*)(Language: )(\w{3})(.*)", "$4") ' source language
                dt2row.Item(3) = Regex.Replace(CmString, "(.*)(\w{3})(-)(\w{3})(.*)", "$4") ' target language
                dt2row.Item(4) = Regex.Replace(CmString, "(.*)(Context Match: )(.*)(;)", "$3") ' Context Match
    
            Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    Re: How to put certain textstrings to certain columns same row?

    Quote Originally Posted by dday9 View Post
    One thing that I notice is that you're getting the same object 5 times:
    Code:
    dt2row.Item(0).ToString()
    Instead, you should be storing that value in a variable(which you're doing) and then getting your substrings from that variable(which you're not doing).

    As far as setting the cell value of the same DataRow that you're currently iterating then it's as easy as getting the value only instead of just getting the value you'd be setting it:
    Code:
            For Each dt2row As DataRow In HashDataTable4.Rows
    
                Dim CmString As String = (dt2row.Item(0).ToString()) ' Here is how I get my textstring.
    
                dt2row.Item(1) = Regex.Replace(CmString, "(\d{5})", "$1") ' ordernr
                dt2row.Item(2) = Regex.Replace(CmString, "(\d{5})(.*)(Language: )(\w{3})(.*)", "$4") ' source language
                dt2row.Item(3) = Regex.Replace(CmString, "(.*)(\w{3})(-)(\w{3})(.*)", "$4") ' target language
                dt2row.Item(4) = Regex.Replace(CmString, "(.*)(Context Match: )(.*)(;)", "$3") ' Context Match
    
            Next
    is this the way to add a column property?


    Code:
    table.Columns.Add("Medication", GetType(String))
    table.Columns.Add("Patient", GetType(String))

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: How to put certain textstrings to certain columns same row?

    If you want to add a column to your DataTable then yes. However, if they're all Strings then it'd be easier to write:
    Code:
    table.Columns.AddRange({New DataColumn("Medication"), New DataColumn("Patient")})
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    Re: How to put certain textstrings to certain columns same row?

    this is what I got so far I but I don't success

    Code:
     CmTable.Columns.Add("Context Matches", GetType(Integer))
            CmTable.Columns.Add("Source language", GetType(String))
            CmTable.Columns.Add("Target language", GetType(String))
            CmTable.Columns.Add("Order number", GetType(String))
    
            For Each dt2row As DataRow In HashDataTable4.Rows
    
             
    
    
                Dim CmString As String = (dt2row.Item(0).ToString()) ' Here is how I get my textstring.
    
    
    
                Dim ordernr As String = Regex.Replace(CmString, "(.*)(\d{5})(.*)(;)", "$2") ' ordernr
                Dim SoLang As String = Regex.Replace(CmString, "(.*)(\d{5})(.*)(Language: )(\w{3})(.*)", "$5") ' source language
                Dim TarLang As String = Regex.Replace(CmString, "(.*)(\w{3})(-)(\w{3})(.*)", "$4") ' target language
                Dim Cm As String = Regex.Replace(CmString, "(.*)(Context Match: )(.*)(;)", "$3") ' Context Match

  6. #6

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    Re: How to put certain textstrings to certain columns same row?

    my bad I solved it thank you.
    Code:
            CmTable.Columns.Add("Order number", GetType(String))
            CmTable.Columns.Add("Source language", GetType(String))
            CmTable.Columns.Add("Target language", GetType(String))
            CmTable.Columns.Add("Context Matches", GetType(Integer))
    
            For Each dt2row As DataRow In HashDataTable4.Rows
    
                Dim CmString As String = (dt2row.Item(0).ToString()) ' Here is how I get my textstring.
                Dim ordernr As String = Regex.Replace(CmString, "(.*)(\d{5})(.*)(;)", "$2") ' ordernr
                Dim SoLang As String = Regex.Replace(CmString, "(.*)(\d{5})(.*)(Language: )(\w{3})(.*)", "$5") ' source language
                Dim TarLang As String = Regex.Replace(CmString, "(.*)(\w{3})(-)(\w{3})(.*)", "$4") ' target language
                Dim Cm As String = Regex.Replace(CmString, "(.*)(Context Match: )(.*)(;)", "$3") ' Context Match
    
                CmTable.Rows.Add(ordernr, SoLang, TarLang, Cm)
            Next
    
            CmTable.DefaultView.Sort = "Order number" ' sorterar kolumnen
            DataGridView2.DataSource = CmTable

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width