Hi all,

I have a requirement as follows,

I have to display a list of datas in to a list box in button click functionality. When i click the button, values should be fetched from database table and loaded in to the list box.
for example: the database table has two values say,
item number
apple 10
pineapple 20
orange 10
jackfruit 25

i am succesfull in fetching the data and displaying it in the list in following way,
apple 10
pineapple 20
orange 10
jackfruit 25

Now i need to align it in the following way in to the listbox

apple              10 
pineapple          20
orange             10
jackfruit          25

how can i do this ?

Thanks in Advance
VBCoder123456

Dani AI

Generated

Short answer: alignment depends on the platform. For a quick fix the simplest route is to render each item with a fixed-width font and pad the first field; the proper fix is to use a control that supports columns (ListView in WinForms or GridView/ListView/Repeater in ASP.NET). As requested code examples, and following 's note that formatting methods work, a few practical options are shown below.

Quick fix (keep a plain ListBox): switch the ListBox font to a monospaced face (for example "Courier New") and build each line using padding so columns line up.

' VB: pad the name to 20 characters, then append quantity
Dim name As String = reader.GetString(0)
Dim qty As String = reader(1).ToString()
Dim line As String = name.PadRight(20) & qty
listBox.Items.Add(line)

Recommended (tabular) options

  • WinForms ListView (details view) — true columns, no padding hacks:

    listView.View = View.Details
    listView.Columns.Add("Item", 200)
    listView.Columns.Add("Qty", 60)
    Dim lvi As New ListViewItem(name)
    lvi.SubItems.Add(qty)
    listView.Items.Add(lvi)
  • ASP.NET GridView — renders a real table on the page:

    
    <asp:GridView ID="gvItems" runat="server" AutoGenerateColumns="False">
    <Columns>
      <asp:BoundField DataField="Item" HeaderText="Item" />
      <asp:BoundField DataField="Qty" HeaderText="Qty" />
    </Columns>
    </asp:GridView>

' Code-behind:
Dim dt As New DataTable()
dt.Load(reader)
gvItems.DataSource = dt
gvItems.DataBind()



Notes and troubleshooting: String.Format (as suggested by @selvaganapathy) is fine for composing the string but still requires a monospaced font to align in a ListBox. In HTML, multiple spaces collapse, so using a web ListBox or <select> with space padding will not work—use a table/GridView or CSS with preserved whitespace. For varying name lengths, choose the pad width to accommodate the longest expected value or use a column control to avoid brittle formatting.

Recommended Answers

All 5 Replies

>> i am succesfull in fetching the data and displaying it in the list in following way,
- post your code

inside the button click i have written the following code

cmd = New SqlCommand("select * from  table", conn)
                billdatard = cmd.ExecuteReader()
                billListBox.Refresh()
                billListBox.Items.Clear()
                While billdatard.Read()
                    firstval = billdatard(0)
                    secondval = billdatard(1)
                    totval = firstval + 				System.Convert.ToString(secondval)
                    billListBox.Items.Add(totval)
		end while

i am able to concatenate the values, but i am not able to align it as mentioned above...

Hi, You can try String.Format()

Try this

Dim str As String
        'Here 20 is the width of the first string
        '-means Left Align, 0 and 1 are index
        str = String.Format("{0,-20} {1,2}", "Hi", 10)
        MessageBox.Show(str)

sir,
This code where i will put please show me

at the insert query or any other code


Thank you

hi realback..
Please do not resurrect old thread..just make your own thread.

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.