How do I add all items form my database to my listbox and a counter?

57 Views Asked by At

I am working on a windows form application project and I am trying to add a column of items form a database into a listbox and have a counter before the item and I cant use the Id form my database because my Id is 1,7,8...20:

private void button2_Click(object sender, EventArgs e)
{
    using (adaptor = new SqlDataAdapter("SELECT * FROM [Table]", con))
    {
        DataTable table = new DataTable();
        adaptor.Fill(table);
        Titul_Carti.ValueMember = "Id";
        for(int i=0;i<table.Rows.Count;i++)
        {
            Titul_Carti.DisplayMember=(i+1).ToString() + " " + table.Rows[i]["numecarte"].ToString();
        }
        Titul_Carti.DataSource=table;
    }
}

Titlu_Carti is my listbox, numecarte is the column I am trying to add to my list and button2 is Monify in my Form.

The problem is that in my listbox I see System.Data.DataRowView and my ValueMember is what I want The first element in the listbox should be kh

I also tried using the Add function(Titul_Carti.Items.Add((i + 1).ToString() + " " + table.Rows[i]["numecarte"].ToString());) but the listbox only showed a counter of 1 through 14

Sorry if I got something wrong I am new here

0

There are 0 best solutions below