Search code examples
c#sql-serverwinformslistboxtext-alignment

Align data from database to listbox


I tried to align data in listbox using \t but did'nt works for a data that is long

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
        {
            SqlDataReader dr = SqlCommand.ExecuteReader();
            while (dr.Read())
            {
                listBox1.Items.Add((string)dr["LastName"] + "\t\t" + dr["ID"]);
            }
        }
    }

Result:

1
(source: akamaihd.net)

How to align the data like this in listbox?

2
(source: akamaihd.net)

I have to used listbox instead of using datagridview or listview for some purposes.


Solution

  • You should know the exact max length of the last name (designed in your table) and apply the appropriate length, such as + 10. Here I use 50 (for the max length) for demonstrative purpose.

    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
        {
            SqlDataReader dr = SqlCommand.ExecuteReader();
            while (dr.Read())
            {
                listBox1.Items.Add(dr["LastName"].ToString().PadRight(50) + dr["ID"]);
            }
        }
    }
    

    Sorry, I didn't test it, but as rene said, using Fixed width font would help. However I have another solution using DrawItem, this is incomplete but can get you started, to complete it I think we need more test and custom code:

    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand SqlCommand = new SqlCommand("Select ID, LastName from Employee", myDatabaseConnection))
        {
            SqlDataReader dr = SqlCommand.ExecuteReader();
            while (dr.Read())
            {
                listBox1.Items.Add(string.Format("{0}\n{1}",dr["LastName"],dr["ID"]));
            }
        }
    }
    //DrawItem
    //first, set listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {            
            e.DrawBackground();
            string[] ss = listBox1.Items[e.Index].ToString().Split(new char[]{'\n'});
            Rectangle rect = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int) (e.Bounds.Width * 0.5), e.Bounds.Height);
            Rectangle rect2 = new Rectangle((int)(e.Bounds.Width * 0.5), e.Bounds.Top, e.Bounds.Width - (int)(e.Bounds.Width * 0.5), e.Bounds.Height);
            StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center };
            e.Graphics.DrawString(ss[0], listBox1.Font, new SolidBrush(listBox1.ForeColor), rect,sf);
            e.Graphics.DrawString(ss[1], listBox1.Font, new SolidBrush(listBox1.ForeColor), rect2, sf);
        } 
    

    As I said, that's just for you to get started, not complete and perfect code to use. For example, I used 50% width of list box for drawing the first 'virtual column' and the remaining for the second 'virtual column'. So that's your part to customize it.