Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-column data in ListBox

I want to have multi-column in my ListBox. Below is the example of picture I got in my application.

Picture of a ListBox that does not have multi-columns

I actually have about 7 columns, but printed out only two columns to make it easier to understand.

So, the first column would say date and the second column would say name. As you can see, the data did not go into their own columns.

This is my code:

this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
// 
// listBox1
// 
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;

foreach (XmlNode xn in xnList)
{
    string date = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "Date").FirstChild.Value;
    string id = xn.OfType<XmlNode>().FirstOrDefault(n => n.Name == "ID").FirstChild.Value;
    if (date == cari)
    {
        this.listBox1.Items.AddRange(new object[] {                    
        //dateBox.Text,
        dateBox.Text + "\r\n" + date});

        this.listBox1.Items.AddRange(new object[] {                    
        "sarabrown"});
    }
}
this.listBox1.Location = new System.Drawing.Point(12, 28);
this.listBox1.MultiColumn = true;
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(300, 95);
this.listBox1.TabIndex = 0;
this.listBox1.ColumnWidth = 100;
// 
// Form3
// 
this.ClientSize = new System.Drawing.Size(400, 273);
this.Controls.Add(this.listBox1);
this.Name = "Form3";
this.ResumeLayout(false);

I found this code there, but it creates a list box that looks just like the one pictured above. Is there anyone knows about this?

like image 819
sara brown Avatar asked Aug 09 '12 04:08

sara brown


People also ask

Can a ListBox have multiple columns?

A multicolumn ListBox places items into as many columns as are needed to make vertical scrolling unnecessary. The user can use the keyboard to navigate to columns that are not currently visible.

How many columns can a ListBox have?

A maximum of 10 columns can be added to the listbox with the VBA AddItem method.

How do I add data to a ListBox in Excel VBA?

There are 3 ways to add items to the VBA Listbox: One at a time using the AddItem property. Adding an array/range using the List property. Adding a Range using the RowSource property.

How do I add a column header to a ListBox in Excel VBA?

Place the ListBox directly on top of the ComboBox. In your VBA, load ListBox row1 with the desired headers. When you click on the listbox, the combobox will drop down and function normally while the headings (in the listbox) remain above the list.


1 Answers

The MultiColumn property of ListBox only helps to avoid vertical scrolling hence just stacks the overflowing items into the next column. The requirement you have is not available by default in .NET. Hence you may have to build your own custom control to support that.

Btw, GridView is your friend.. What you need is easily achievable using GridView. For e.g., to make it simplistic (you may have to tweak this entirely to suit your problem)

protected void MyGridView_PreRender(object sender, EventArgs e)
{
    DataSet myDataSet = new DataSet();
    myDataSet.ReadXml(new StringReader(myXmlDoc.OuterXml));
    GridView gv = (GridView)sender;
    gv.DataSource = myDataSet;
    gv.DataBind();
}

UPDATE:

You may want to check out ListView instead of GridView or ListBox. It is comparatively lightweight than a GridView.

With ListView you can also put in other controls in the different columns like checkboxes.

Check this example out to give you an idea.
Or this one which is simpler: Using ListView control in C#.

like image 121
Kash Avatar answered Oct 27 '22 03:10

Kash