Multi-column data in ListBox

75,930

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#.

Share:
75,930
sara brown
Author by

sara brown

Updated on July 19, 2022

Comments

  • sara brown
    sara brown almost 2 years

    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?

    • lc.
      lc. almost 12 years
      Do you have anything against using a DataGridView?
    • SMK
      SMK almost 12 years
    • nawfal
      nawfal over 10 years
      possible duplicate of C# MultiColumn Listbox
    • ToolmakerSteve
      ToolmakerSteve about 3 years
      Use ListView instead.
    • Marcelo Scofano Diniz
      Marcelo Scofano Diniz over 2 years
      @ToolmakerSteve and all, although OP did not stated this need, ListView would be a fine choice if it has DataBinding through DataSource collection... any updates that I'm not aware?
    • ToolmakerSteve
      ToolmakerSteve over 2 years
      @MarceloScofanoDiniz - I build mine using a loop in code. My point here was simply that ListView has a way to describe multiple columns.
  • sara brown
    sara brown almost 12 years
    i have tried this and it says "cannot convert from [,] to []". then i tried to remove the ',' and it prints out the same.
  • Kash
    Kash almost 12 years
    @sarabrown: that code would not work because it is not supported in .NET. The MultiColumn property is meant for something else as described above (I know, it is misleading).
  • mathias.horn
    mathias.horn over 6 years
    No. This is not multicolumn. The given example add a range of values, one per row.