2

is it possible to display the two database column in one row column in listview in vb.net using mysql.data.mysqlclient?

 Listview:              where in database:
 ---------------            *Firstname: John
 |  Full Name  |            *Lastname: Smith
 |_____________|
 |             |
 |  John Smith |
 |_____________|

here's my code:

For i = 0 To table.Rows.Count - 1
    With lvlistview
        .Items.Add(table.Rows(i)("dte"))
        With .Items(.Items.Count - 1).SubItems
            .Add(table.Rows(i)("tran_no"))
            .Add(table.Rows(i)("comp_type"))
            .Add(table.Rows(i)("status"))
            .Add(table.Rows(i)("sys_name"))
            .Add(table.Rows(i)("mod_name"))
            .Add(table.Rows(i)("err_desc"))
            .Add(table.Rows(i)("trigg"))
            .Add(table.Rows(i)("fname" . "lname")) **How i gonna combine this two database column in only one listview column**
        End With
    End With
Next
5
  • How are you populating the listview control? With data-binding? Is this a WinForm, ASP, WPF project? Commented Dec 2, 2013 at 14:04
  • it's winform sir. how i gonna do this? put two database column in one listview column? Commented Dec 2, 2013 at 14:05
  • How are you populating the listview control? Are you using data-binding, or are you manually adding the items with your own code? Commented Dec 2, 2013 at 14:06
  • i manually adding the items in my own code sir. Commented Dec 2, 2013 at 14:07
  • Can you show that part of your code? Commented Dec 2, 2013 at 14:08

1 Answer 1

1

You simply need to concatenate the values from the two DB columns together. If you were using data-binding, the simplest solution would be to alter your SQL command so that the values are concatenated by the DB engine and returned to you as a single column. However, since you are adding the items yourself, you can, alternatively, just concatenate the two columns in your code before adding the item to the ListView, for instance:

With .Items(.Items.Count - 1).SubItems
    .Add(table.Rows(i)("tran_no"))
    .Add(table.Rows(i)("comp_type"))
    .Add(table.Rows(i)("status"))
    .Add(table.Rows(i)("sys_name"))
    .Add(table.Rows(i)("mod_name"))
    .Add(table.Rows(i)("err_desc"))
    .Add(table.Rows(i)("trigg"))
    .Add(table.Rows(i)("fname") & " " & table.Rows(i)("lname"))
End With

The & character is the standard string-concatenation operator in VB.NET. You can also use the + operator, but you have to be more careful of your type casting when you use the + operator with strings. Since the & operator is a little safer and it's slightly more self-documenting, it's typically the preferred operator for strings.

For more complicated concatenations, you may also want to consider using String.Join, String.Format or the StringBuilder class.

Sign up to request clarification or add additional context in comments.

2 Comments

THANK YOU SIR!!!!! it worked!! thank you so much. i have one last question. do you know how to left join in listview using winform?
The easiest thing would be to do the left join in your SQL command, then you'll have access to the joined columns via the table variable, just like the other columns. Alternatively, you could execute two separate SQL commands to get the data into two separate data tables, then you could use both data tables to populate each ListView item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.