Home > Software design >  Is there a way that I can format data in a listbox into columns when the data is from an SQLITE data
Is there a way that I can format data in a listbox into columns when the data is from an SQLITE data

Time:04-05

This is what i've currently tried but I get the error statement showed below. ITEMNAME, ITEMQUANTITY and ITEMPRICE are what's being inputted into the listbox from the database.

        AllItems.Items.Clear();
        AllItems.Items.Add(String.Format(listboxformatting, "Item Name", "Item Quantity", "Item Price (£)"));
        sqlite_conn = new SQLiteConnection("Data Source=RetailSystem.db; Version = 3; New = True; Compress = True;");
        try
        {
            SQLiteDataReader sqlite_datareadert;
            sqlite_conn.Open();
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "SELECT * FROM TblItemStock";
            sqlite_datareadert = sqlite_cmd.ExecuteReader();
            while (sqlite_datareadert.Read())
            {
                string ITEMNAME = sqlite_datareadert.GetString(0);
                int ITEMQUANTITY = sqlite_datareadert.GetInt32(1);
                decimal ITEMPRICE = sqlite_datareadert.GetDecimal(2);

                AllItems.Items.Add(String.Format(listboxformatting,  ITEMNAME   ITEMQUANTITY   ITEMPRICE));
                AllItems.Refresh();
            }
            sqlite_datareadert.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

ErrorCode

What Get's Displayed

CodePudding user response:

If you want to drop a datagridview on your form and rename it to _itemsDataGridView this should be fine:

    try
    {
        var da = new SQLiteDataAdapter("SELECT * FROM TblItemStock", "Data Source=RetailSystem.db;Version=3;New=True;Compress=True;")

        var dt = new DataTable();
        da.Fill(dt);
        _itemsDataGridView.DataSource = dt;
   
        _itemsDataGridView.Columns[0].HeaderText = "Item Name";
        _itemsDataGridView.Columns[1].HeaderText = "Item Quantity";
        _itemsDataGridView.Columns[2].HeaderText = "Item Price (£)";
        
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
  • Related