简体   繁体   中英

Transform integer to numeric list in combobox from db - C#

I have a table called "Shoppings" in my SQL db. This one has a column called qtPisos, which describes how many levels a shopping mall has. In my program I would like to create a combobox based on the quantity of this integer, so the user can select the level in the shopping mall, which will then list the shops for that particular level.

Any idea how can I do that?

Here is my code.

public partial class Shoppings : Form
{
    private DataViewManager dsView;
    private DataSet ds;

    public Shoppings()
    {
        InitializeComponent();
    }

    private void Shoppings_Load(object sender, EventArgs e)
    {
        Login logForm = new Login();
        logForm = logForm.getLoginForm();
        ds = new DataSet("DsShoppings");

        // Fill the Dataset with Shoppings, map Default Tablename "Table" to "Shoppings".
        SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Shoppings", logForm.sqlConnection);
        sda.TableMappings.Add("Table", "Shoppings");
        sda.Fill(this.ds);

        // The DataViewManager returned by the DefaultViewManager property
        // allows to create custom settings for each DataTable in the DataSet.
        this.dsView = ds.DefaultViewManager;

        // Combobox Databinding
        ComboShopping.DataSource = this.dsView;
        ComboShopping.DisplayMember = "Shoppings.nomeShopping";
        ComboShopping.ValueMember = "Shoppings.idShopping";

        // Text Columns DataBinding
        txtTotalLojas.DataBindings.Add("Text", dsView, "Shoppings.totalLojas");

        //int qtPisos = Convert.ToInt32(ds.Tables["Shoppings"].Rows[0]["qtPisos"]);
        ComboPisos.DataSource = this.dsView;

You can retrieve the value of the levels and then create a numeric array using this value as the maximum value:

var levels = Enumerable.Range(1, levels)

You can then bind this value to a DropDownlist control or such.

control.Source = levels;
control.Bind()

To then display a list of shops for that level, you could use the DropDownList's autopostback feature and retrieve your shops based upon the SelectedValue property of the control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM