0

I am using Visual Studio and creating an ASP.NET WebForm using C#.

I have a ListBox that needs to have multiple columns populated from multiple tables from a SQL Server database. Ex, a listbox that shows something like:

[Food.Name], [Food.ID], [Manufacturer.Name], [Origin.City]
[Banana],       [1],         [Chiquita],     [Buenos Aires]
[Spaghetti],    [2],         [Bertolli],         [Rome]

So far in my attempts I am only able to get one column to show up even with a correct sql statement. The SQL statement is not the issue, but rather the limitations of the ListBox and datasource.

Do I need to make a datasource for each individual table and concatenate the data into variables and use those in the listbox somehow?

I tried to simply edit the sql statement created when editing the datasource properties, but I only get one column outputted. I am not sure how to grab data from multiple tables.

I have truly tried to find this answer on the internet but I don't know enough about what I'm doing to find the answer. Thanks for the help.

3
  • Are you looking for SQL Joins? You should probably be using some kind of grid able to display more than one column instead of a ListBox. Commented Nov 22, 2022 at 19:50
  • I have unfortunately been told to use a list box control for an assignment. I will probably need joins, but I'm not sure how to do this in a single listbox. Commented Nov 22, 2022 at 20:03
  • Create a class with one property for each column. Then override ToString to display a string consisting of the concatenated columns. Add the objects to a List<TheClass> and use it as data source. Commented Nov 22, 2022 at 20:09

1 Answer 1

0

Well, the problem here is a listbox has the ability to have a "hidden" column (usually the database PK id), and then one column for display.

Since a listbox always shows (unlike a combo/dropdown list), then it stands to reason that using a listview, or gridview is a MUCH better choice.

Not clear why someone would suggest using a control that does not support multiple columns, and then you being told to display multiple columns? (some kind of drunken rodeo clown is clearly asking you to do something that makes no more sense than demonstrating to driving a car in reverse down a busy freeway).

ok, so having stated the above? And having stated that a gridview is a FAR better choice?

Well, you need to first choose a "fixed" font size. (Since with multiple columns, there is little hope of the columns to algin up).

so, lets drop in a listview, and fill it with say hotel name, city, and if it is active or not.

So, our list view markup is this:

        <asp:ListBox ID="ListBox1" runat="server"
            DataValueField ="ID"
            DataTextField  = "MyHotel" >
        </asp:ListBox>

And now our code to load up the 3 columns (hotel name, city, and "active"). And of course, the value returned for a selection (not displayed) like all typical listbox will remain the database pk row of "ID".

And we need to pad (use fixed column length).

And, a listbox will gobble up spaces (html), so we need to use "160" (a  ).

so this code:

void LoadData()
{
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
    {
        string strSQL =
            @"SELECT ID, HotelName, City, Active FROM tblHotelsA
                ORDER BY HotelName";

        using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
        {
            conn.Open();
            dt.Load(cmdSQL.ExecuteReader());
            dt.Columns.Add("MyHotel");
            foreach (DataRow dr in dt.Rows)
            {
                string MyHotel =
                    mypad(dr["HotelName"].ToString(), 20) + " |" +
                    mypad(dr["City"].ToString(), 12) + " |" +
                    mypad(dr["Active"].ToString(), 5);

                dr["MyHotel"] = MyHotel;
            }

            ListBox1.DataSource = dt;
            ListBox1.DataBind();
        }
    }
}

public string mypad(string s, int padlen)
{
    int iLen = padlen - s.Length;
    if (iLen < 0)
        iLen = 0;

    string sResult = s + string.Concat(Enumerable.Repeat(Convert.ToChar(160), iLen));

    return sResult;
}

And we now see/get this:

enter image description here

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

Comments

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.