开发者

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

开发者 https://www.devze.com 2023-03-06 14:33 出处:网络
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 th

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", "Sh开发者_如何学编程oppings");
        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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号