繁体   English   中英

从db将整数转换为组合框中的数字列表-C#

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

我的SQL数据库中有一个名为“购物”的表。 这个有一个叫做qtPisos的列,它描述了一个购物中心有多少层。 在我的程序中,我想基于此整数的数量创建一个组合框,以便用户可以在购物中心中选择级别,然后将列出该级别的商店。

知道我该怎么做吗?

这是我的代码。

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;

您可以检索级别的值,然后使用此值作为最大值创建数字数组:

var levels = Enumerable.Range(1, levels)

然后,您可以将此值绑定到DropDownlist控件等。

control.Source = levels;
control.Bind()

若要显示该级别的商店列表,可以使用DropDownList的自动回发功能,并根据控件的SelectedValue属性检索商店。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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